{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AdaGrad算法\n",
    "\n",
    "在之前介绍过的优化算法中，目标函数自变量的每一个元素在相同时间步都使用同一个学习率来自我迭代。举个例子，假设目标函数为$f$，自变量为一个二维向量$[x_1, x_2]^\\top$，该向量中每一个元素在迭代时都使用相同的学习率。例如，在学习率为$\\eta$的梯度下降中，元素$x_1$和$x_2$都使用相同的学习率$\\eta$来自我迭代：\n",
    "\n",
    "$$\n",
    "x_1 \\leftarrow x_1 - \\eta \\frac{\\partial{f}}{\\partial{x_1}}, \\quad\n",
    "x_2 \\leftarrow x_2 - \\eta \\frac{\\partial{f}}{\\partial{x_2}}.\n",
    "$$\n",
    "\n",
    "在[“动量法”](./momentum.ipynb)一节里我们看到，当$x_1$和$x_2$的梯度值有较大差别时，需要选择足够小的学习率使得自变量在梯度值较大的维度上不发散。但这样会导致自变量在梯度值较小的维度上迭代过慢。动量法依赖指数加权移动平均使得自变量的更新方向更加一致，从而降低发散的可能。本节我们介绍AdaGrad算法，它根据自变量在每个维度的梯度值的大小来调整各个维度上的学习率，从而避免统一的学习率难以适应所有维度的问题 [1]。\n",
    "\n",
    "\n",
    "## 算法\n",
    "\n",
    "AdaGrad算法会使用一个小批量随机梯度$\\boldsymbol{g}_t$按元素平方的累加变量$\\boldsymbol{s}_t$。在时间步0，AdaGrad将$\\boldsymbol{s}_0$中每个元素初始化为0。在时间步$t$，首先将小批量随机梯度$\\boldsymbol{g}_t$按元素平方后累加到变量$\\boldsymbol{s}_t$：\n",
    "\n",
    "$$\\boldsymbol{s}_t \\leftarrow \\boldsymbol{s}_{t-1} + \\boldsymbol{g}_t \\odot \\boldsymbol{g}_t,$$\n",
    "\n",
    "其中$\\odot$是按元素相乘。接着，我们将目标函数自变量中每个元素的学习率通过按元素运算重新调整一下：\n",
    "\n",
    "$$\\boldsymbol{x}_t \\leftarrow \\boldsymbol{x}_{t-1} - \\frac{\\eta}{\\sqrt{\\boldsymbol{s}_t + \\epsilon}} \\odot \\boldsymbol{g}_t,$$\n",
    "\n",
    "其中$\\eta$是学习率，$\\epsilon$是为了维持数值稳定性而添加的常数，如$10^{-6}$。这里开方、除法和乘法的运算都是按元素运算的。这些按元素运算使得目标函数自变量中每个元素都分别拥有自己的学习率。\n",
    "\n",
    "## 特点\n",
    "\n",
    "需要强调的是，小批量随机梯度按元素平方的累加变量$\\boldsymbol{s}_t$出现在学习率的分母项中。因此，如果目标函数有关自变量中某个元素的偏导数一直都较大，那么该元素的学习率将下降较快；反之，如果目标函数有关自变量中某个元素的偏导数一直都较小，那么该元素的学习率将下降较慢。然而，由于$\\boldsymbol{s}_t$一直在累加按元素平方的梯度，自变量中每个元素的学习率在迭代过程中一直在降低（或不变）。所以，当学习率在迭代早期降得较快且当前解依然不佳时，AdaGrad算法在迭代后期由于学习率过小，可能较难找到一个有用的解。\n",
    "\n",
    "下面我们仍然以目标函数$f(\\boldsymbol{x})=0.1x_1^2+2x_2^2$为例观察AdaGrad算法对自变量的迭代轨迹。我们实现AdaGrad算法并使用和上一节实验中相同的学习率0.4。可以看到，自变量的迭代轨迹较平滑。但由于$\\boldsymbol{s}_t$的累加效果使学习率不断衰减，自变量在迭代后期的移动幅度较小。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "2"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 20, x1 -2.382563, x2 -0.158591\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Created with matplotlib (http://matplotlib.org/) -->\n",
       "<svg height=\"184.15625pt\" version=\"1.1\" viewBox=\"0 0 248.620313 184.15625\" width=\"248.620313pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <defs>\n",
       "  <style type=\"text/css\">\n",
       "*{stroke-linecap:butt;stroke-linejoin:round;}\n",
       "  </style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 184.15625 \n",
       "L 248.620313 184.15625 \n",
       "L 248.620313 -0 \n",
       "L 0 -0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 42.620312 146.6 \n",
       "L 237.920313 146.6 \n",
       "L 237.920313 10.7 \n",
       "L 42.620312 10.7 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"me711a76d87\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"88.39375\" xlink:href=\"#me711a76d87\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- −4 -->\n",
       "      <defs>\n",
       "       <path d=\"M 10.59375 35.5 \n",
       "L 73.1875 35.5 \n",
       "L 73.1875 27.203125 \n",
       "L 10.59375 27.203125 \n",
       "z\n",
       "\" id=\"DejaVuSans-2212\"/>\n",
       "       <path d=\"M 37.796875 64.3125 \n",
       "L 12.890625 25.390625 \n",
       "L 37.796875 25.390625 \n",
       "z\n",
       "M 35.203125 72.90625 \n",
       "L 47.609375 72.90625 \n",
       "L 47.609375 25.390625 \n",
       "L 58.015625 25.390625 \n",
       "L 58.015625 17.1875 \n",
       "L 47.609375 17.1875 \n",
       "L 47.609375 0 \n",
       "L 37.796875 0 \n",
       "L 37.796875 17.1875 \n",
       "L 4.890625 17.1875 \n",
       "L 4.890625 26.703125 \n",
       "z\n",
       "\" id=\"DejaVuSans-34\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(81.022656 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"149.425\" xlink:href=\"#me711a76d87\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- −2 -->\n",
       "      <defs>\n",
       "       <path d=\"M 19.1875 8.296875 \n",
       "L 53.609375 8.296875 \n",
       "L 53.609375 0 \n",
       "L 7.328125 0 \n",
       "L 7.328125 8.296875 \n",
       "Q 12.9375 14.109375 22.625 23.890625 \n",
       "Q 32.328125 33.6875 34.8125 36.53125 \n",
       "Q 39.546875 41.84375 41.421875 45.53125 \n",
       "Q 43.3125 49.21875 43.3125 52.78125 \n",
       "Q 43.3125 58.59375 39.234375 62.25 \n",
       "Q 35.15625 65.921875 28.609375 65.921875 \n",
       "Q 23.96875 65.921875 18.8125 64.3125 \n",
       "Q 13.671875 62.703125 7.8125 59.421875 \n",
       "L 7.8125 69.390625 \n",
       "Q 13.765625 71.78125 18.9375 73 \n",
       "Q 24.125 74.21875 28.421875 74.21875 \n",
       "Q 39.75 74.21875 46.484375 68.546875 \n",
       "Q 53.21875 62.890625 53.21875 53.421875 \n",
       "Q 53.21875 48.921875 51.53125 44.890625 \n",
       "Q 49.859375 40.875 45.40625 35.40625 \n",
       "Q 44.1875 33.984375 37.640625 27.21875 \n",
       "Q 31.109375 20.453125 19.1875 8.296875 \n",
       "z\n",
       "\" id=\"DejaVuSans-32\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(142.053906 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"210.45625\" xlink:href=\"#me711a76d87\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 31.78125 66.40625 \n",
       "Q 24.171875 66.40625 20.328125 58.90625 \n",
       "Q 16.5 51.421875 16.5 36.375 \n",
       "Q 16.5 21.390625 20.328125 13.890625 \n",
       "Q 24.171875 6.390625 31.78125 6.390625 \n",
       "Q 39.453125 6.390625 43.28125 13.890625 \n",
       "Q 47.125 21.390625 47.125 36.375 \n",
       "Q 47.125 51.421875 43.28125 58.90625 \n",
       "Q 39.453125 66.40625 31.78125 66.40625 \n",
       "z\n",
       "M 31.78125 74.21875 \n",
       "Q 44.046875 74.21875 50.515625 64.515625 \n",
       "Q 56.984375 54.828125 56.984375 36.375 \n",
       "Q 56.984375 17.96875 50.515625 8.265625 \n",
       "Q 44.046875 -1.421875 31.78125 -1.421875 \n",
       "Q 19.53125 -1.421875 13.0625 8.265625 \n",
       "Q 6.59375 17.96875 6.59375 36.375 \n",
       "Q 6.59375 54.828125 13.0625 64.515625 \n",
       "Q 19.53125 74.21875 31.78125 74.21875 \n",
       "z\n",
       "\" id=\"DejaVuSans-30\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(207.275 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_4\">\n",
       "     <!-- x1 -->\n",
       "     <defs>\n",
       "      <path d=\"M 54.890625 54.6875 \n",
       "L 35.109375 28.078125 \n",
       "L 55.90625 0 \n",
       "L 45.3125 0 \n",
       "L 29.390625 21.484375 \n",
       "L 13.484375 0 \n",
       "L 2.875 0 \n",
       "L 24.125 28.609375 \n",
       "L 4.6875 54.6875 \n",
       "L 15.28125 54.6875 \n",
       "L 29.78125 35.203125 \n",
       "L 44.28125 54.6875 \n",
       "z\n",
       "\" id=\"DejaVuSans-78\"/>\n",
       "      <path d=\"M 12.40625 8.296875 \n",
       "L 28.515625 8.296875 \n",
       "L 28.515625 63.921875 \n",
       "L 10.984375 60.40625 \n",
       "L 10.984375 69.390625 \n",
       "L 28.421875 72.90625 \n",
       "L 38.28125 72.90625 \n",
       "L 38.28125 8.296875 \n",
       "L 54.390625 8.296875 \n",
       "L 54.390625 0 \n",
       "L 12.40625 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-31\"/>\n",
       "     </defs>\n",
       "     <g transform=\"translate(134.129687 174.876562)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"59.179688\" xlink:href=\"#DejaVuSans-31\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m0367fc4cdc\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m0367fc4cdc\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- −3 -->\n",
       "      <defs>\n",
       "       <path d=\"M 40.578125 39.3125 \n",
       "Q 47.65625 37.796875 51.625 33 \n",
       "Q 55.609375 28.21875 55.609375 21.1875 \n",
       "Q 55.609375 10.40625 48.1875 4.484375 \n",
       "Q 40.765625 -1.421875 27.09375 -1.421875 \n",
       "Q 22.515625 -1.421875 17.65625 -0.515625 \n",
       "Q 12.796875 0.390625 7.625 2.203125 \n",
       "L 7.625 11.71875 \n",
       "Q 11.71875 9.328125 16.59375 8.109375 \n",
       "Q 21.484375 6.890625 26.8125 6.890625 \n",
       "Q 36.078125 6.890625 40.9375 10.546875 \n",
       "Q 45.796875 14.203125 45.796875 21.1875 \n",
       "Q 45.796875 27.640625 41.28125 31.265625 \n",
       "Q 36.765625 34.90625 28.71875 34.90625 \n",
       "L 20.21875 34.90625 \n",
       "L 20.21875 43.015625 \n",
       "L 29.109375 43.015625 \n",
       "Q 36.375 43.015625 40.234375 45.921875 \n",
       "Q 44.09375 48.828125 44.09375 54.296875 \n",
       "Q 44.09375 59.90625 40.109375 62.90625 \n",
       "Q 36.140625 65.921875 28.71875 65.921875 \n",
       "Q 24.65625 65.921875 20.015625 65.03125 \n",
       "Q 15.375 64.15625 9.8125 62.3125 \n",
       "L 9.8125 71.09375 \n",
       "Q 15.4375 72.65625 20.34375 73.4375 \n",
       "Q 25.25 74.21875 29.59375 74.21875 \n",
       "Q 40.828125 74.21875 47.359375 69.109375 \n",
       "Q 53.90625 64.015625 53.90625 55.328125 \n",
       "Q 53.90625 49.265625 50.4375 45.09375 \n",
       "Q 46.96875 40.921875 40.578125 39.3125 \n",
       "z\n",
       "\" id=\"DejaVuSans-33\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(20.878125 150.399219)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-33\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m0367fc4cdc\" y=\"111.753846\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <!-- −2 -->\n",
       "      <g transform=\"translate(20.878125 115.553065)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m0367fc4cdc\" y=\"76.907692\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- −1 -->\n",
       "      <g transform=\"translate(20.878125 80.706911)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-31\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m0367fc4cdc\" y=\"42.061538\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(29.257812 45.860757)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_9\">\n",
       "     <!-- x2 -->\n",
       "     <g transform=\"translate(14.798437 84.790625)rotate(-90)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"59.179688\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_1\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 237.920313 84.148723 \n",
       "L 234.86875 84.2672 \n",
       "L 231.817188 84.371738 \n",
       "L 228.765625 84.462338 \n",
       "L 225.714063 84.539 \n",
       "L 222.6625 84.601723 \n",
       "L 219.610938 84.650508 \n",
       "L 216.559375 84.685354 \n",
       "L 213.507813 84.706262 \n",
       "L 210.45625 84.713231 \n",
       "L 207.404688 84.706262 \n",
       "L 204.353125 84.685354 \n",
       "L 201.301563 84.650508 \n",
       "L 198.25 84.601723 \n",
       "L 195.198438 84.539 \n",
       "L 192.146875 84.462338 \n",
       "L 189.095313 84.371738 \n",
       "L 186.04375 84.2672 \n",
       "L 182.992188 84.148723 \n",
       "L 179.940625 84.016308 \n",
       "L 177.034375 83.876923 \n",
       "L 176.889063 83.869348 \n",
       "L 173.8375 83.695117 \n",
       "L 170.785938 83.505736 \n",
       "L 167.734375 83.301204 \n",
       "L 164.682813 83.081522 \n",
       "L 161.63125 82.846689 \n",
       "L 158.579688 82.596706 \n",
       "L 155.528125 82.331572 \n",
       "L 152.476563 82.051288 \n",
       "L 149.425 81.755853 \n",
       "L 146.373438 81.445268 \n",
       "L 143.321875 81.119532 \n",
       "L 140.270313 80.778645 \n",
       "L 137.21875 80.422609 \n",
       "L 136.969643 80.392308 \n",
       "L 134.167188 80.018956 \n",
       "L 131.115625 79.595824 \n",
       "L 128.064063 79.156099 \n",
       "L 125.0125 78.69978 \n",
       "L 121.960938 78.226868 \n",
       "L 118.909375 77.737363 \n",
       "L 115.857813 77.231264 \n",
       "L 113.96875 76.907692 \n",
       "L 112.80625 76.687611 \n",
       "L 109.754688 76.091559 \n",
       "L 106.703125 75.477166 \n",
       "L 103.651563 74.844433 \n",
       "L 100.6 74.19336 \n",
       "L 97.548438 73.523947 \n",
       "L 97.100875 73.423077 \n",
       "L 94.496875 72.767149 \n",
       "L 91.445313 71.977986 \n",
       "L 88.39375 71.168326 \n",
       "L 85.342188 70.338167 \n",
       "L 83.908321 69.938462 \n",
       "L 82.290625 69.427385 \n",
       "L 79.239063 68.440077 \n",
       "L 76.1875 67.429538 \n",
       "L 73.307374 66.453846 \n",
       "L 73.135938 66.386834 \n",
       "L 70.084375 65.167219 \n",
       "L 67.032813 63.920799 \n",
       "L 64.752171 62.969231 \n",
       "L 63.98125 62.589091 \n",
       "L 60.929688 61.052692 \n",
       "L 57.878125 59.484615 \n",
       "L 57.878125 59.484615 \n",
       "L 54.826563 57.529359 \n",
       "L 52.486044 56 \n",
       "L 51.775 55.402637 \n",
       "L 48.723438 52.789176 \n",
       "L 48.409725 52.515385 \n",
       "L 45.671875 49.170154 \n",
       "L 45.559891 49.030769 \n",
       "L 43.880132 45.546154 \n",
       "L 43.320212 42.061538 \n",
       "L 43.880132 38.576923 \n",
       "L 45.559891 35.092308 \n",
       "L 45.671875 34.952923 \n",
       "L 48.409725 31.607692 \n",
       "L 48.723438 31.333901 \n",
       "L 51.775 28.72044 \n",
       "L 52.486044 28.123077 \n",
       "L 54.826563 26.593718 \n",
       "L 57.878125 24.638462 \n",
       "L 57.878125 24.638462 \n",
       "L 60.929688 23.070385 \n",
       "L 63.98125 21.533986 \n",
       "L 64.752171 21.153846 \n",
       "L 67.032813 20.202278 \n",
       "L 70.084375 18.955858 \n",
       "L 73.135938 17.736243 \n",
       "L 73.307374 17.669231 \n",
       "L 76.1875 16.693538 \n",
       "L 79.239063 15.683 \n",
       "L 82.290625 14.695692 \n",
       "L 83.908321 14.184615 \n",
       "L 85.342188 13.78491 \n",
       "L 88.39375 12.954751 \n",
       "L 91.445313 12.14509 \n",
       "L 94.496875 11.355928 \n",
       "L 97.100875 10.7 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_2\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 237.920313 101.991945 \n",
       "L 234.86875 102.076571 \n",
       "L 231.817188 102.151242 \n",
       "L 228.765625 102.215956 \n",
       "L 225.714063 102.270714 \n",
       "L 222.6625 102.315516 \n",
       "L 219.610938 102.350363 \n",
       "L 216.559375 102.375253 \n",
       "L 213.507813 102.390187 \n",
       "L 210.45625 102.395165 \n",
       "L 207.404688 102.390187 \n",
       "L 204.353125 102.375253 \n",
       "L 201.301563 102.350363 \n",
       "L 198.25 102.315516 \n",
       "L 195.198438 102.270714 \n",
       "L 192.146875 102.215956 \n",
       "L 189.095313 102.151242 \n",
       "L 186.04375 102.076571 \n",
       "L 182.992188 101.991945 \n",
       "L 179.940625 101.897363 \n",
       "L 176.889063 101.792824 \n",
       "L 173.8375 101.67833 \n",
       "L 170.785938 101.553879 \n",
       "L 167.734375 101.419473 \n",
       "L 165.208944 101.3 \n",
       "L 164.682813 101.273601 \n",
       "L 161.63125 101.10993 \n",
       "L 158.579688 100.935699 \n",
       "L 155.528125 100.750909 \n",
       "L 152.476563 100.555559 \n",
       "L 149.425 100.34965 \n",
       "L 146.373438 100.133182 \n",
       "L 143.321875 99.906154 \n",
       "L 140.270313 99.668566 \n",
       "L 137.21875 99.42042 \n",
       "L 134.167188 99.161713 \n",
       "L 131.115625 98.892448 \n",
       "L 128.064063 98.612622 \n",
       "L 125.0125 98.322238 \n",
       "L 121.960938 98.021294 \n",
       "L 119.943803 97.815385 \n",
       "L 118.909375 97.702978 \n",
       "L 115.857813 97.360136 \n",
       "L 112.80625 97.006055 \n",
       "L 109.754688 96.640732 \n",
       "L 106.703125 96.264169 \n",
       "L 103.651563 95.876365 \n",
       "L 100.6 95.47732 \n",
       "L 97.548438 95.067035 \n",
       "L 94.496875 94.645509 \n",
       "L 92.277557 94.330769 \n",
       "L 91.445313 94.204602 \n",
       "L 88.39375 93.729973 \n",
       "L 85.342188 93.243329 \n",
       "L 82.290625 92.744668 \n",
       "L 79.239063 92.233992 \n",
       "L 76.1875 91.7113 \n",
       "L 73.135938 91.176592 \n",
       "L 71.291587 90.846154 \n",
       "L 70.084375 90.613846 \n",
       "L 67.032813 90.013718 \n",
       "L 63.98125 89.400684 \n",
       "L 60.929688 88.774744 \n",
       "L 57.878125 88.135897 \n",
       "L 54.826563 87.484145 \n",
       "L 54.263653 87.361538 \n",
       "L 51.775 86.776123 \n",
       "L 48.723438 86.044354 \n",
       "L 45.671875 85.298646 \n",
       "L 42.620313 84.539 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_3\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 237.920313 115.639597 \n",
       "L 234.86875 115.708479 \n",
       "L 231.817188 115.769258 \n",
       "L 228.765625 115.821932 \n",
       "L 225.714063 115.866503 \n",
       "L 222.6625 115.90297 \n",
       "L 219.610938 115.931333 \n",
       "L 216.559375 115.951592 \n",
       "L 213.507813 115.963748 \n",
       "L 210.45625 115.9678 \n",
       "L 207.404688 115.963748 \n",
       "L 204.353125 115.951592 \n",
       "L 201.301563 115.931333 \n",
       "L 198.25 115.90297 \n",
       "L 195.198438 115.866503 \n",
       "L 192.146875 115.821932 \n",
       "L 189.095313 115.769258 \n",
       "L 186.04375 115.708479 \n",
       "L 182.992188 115.639597 \n",
       "L 179.940625 115.562612 \n",
       "L 176.889063 115.477522 \n",
       "L 173.8375 115.384329 \n",
       "L 170.785938 115.283032 \n",
       "L 169.542708 115.238462 \n",
       "L 167.734375 115.170469 \n",
       "L 164.682813 115.047233 \n",
       "L 161.63125 114.915497 \n",
       "L 158.579688 114.775263 \n",
       "L 155.528125 114.626529 \n",
       "L 152.476563 114.469296 \n",
       "L 149.425 114.303565 \n",
       "L 146.373438 114.129334 \n",
       "L 143.321875 113.946604 \n",
       "L 140.270313 113.755375 \n",
       "L 137.21875 113.555647 \n",
       "L 134.167188 113.34742 \n",
       "L 131.115625 113.130694 \n",
       "L 128.064063 112.905469 \n",
       "L 125.0125 112.671745 \n",
       "L 121.960938 112.429522 \n",
       "L 118.909375 112.178799 \n",
       "L 115.857813 111.919578 \n",
       "L 113.96875 111.753846 \n",
       "L 112.80625 111.646627 \n",
       "L 109.754688 111.356243 \n",
       "L 106.703125 111.056923 \n",
       "L 103.651563 110.748669 \n",
       "L 100.6 110.431479 \n",
       "L 97.548438 110.105355 \n",
       "L 94.496875 109.770296 \n",
       "L 91.445313 109.426302 \n",
       "L 88.39375 109.073373 \n",
       "L 85.342188 108.711509 \n",
       "L 82.290625 108.34071 \n",
       "L 81.716213 108.269231 \n",
       "L 79.239063 107.944314 \n",
       "L 76.1875 107.534636 \n",
       "L 73.135938 107.115541 \n",
       "L 70.084375 106.687027 \n",
       "L 67.032813 106.249096 \n",
       "L 63.98125 105.801746 \n",
       "L 60.929688 105.344979 \n",
       "L 57.878125 104.878794 \n",
       "L 57.273855 104.784615 \n",
       "L 54.826563 104.381396 \n",
       "L 51.775 103.868659 \n",
       "L 48.723438 103.345967 \n",
       "L 45.671875 102.813319 \n",
       "L 42.620313 102.270714 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_4\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 237.920313 127.111044 \n",
       "L 234.86875 127.171491 \n",
       "L 231.817188 127.224827 \n",
       "L 228.765625 127.271052 \n",
       "L 225.714063 127.310165 \n",
       "L 222.6625 127.342166 \n",
       "L 219.610938 127.367057 \n",
       "L 216.559375 127.384835 \n",
       "L 213.507813 127.395502 \n",
       "L 210.45625 127.399058 \n",
       "L 207.404688 127.395502 \n",
       "L 204.353125 127.384835 \n",
       "L 201.301563 127.367057 \n",
       "L 198.25 127.342166 \n",
       "L 195.198438 127.310165 \n",
       "L 192.146875 127.271052 \n",
       "L 189.095313 127.224827 \n",
       "L 186.04375 127.171491 \n",
       "L 182.992188 127.111044 \n",
       "L 179.940625 127.043485 \n",
       "L 176.889063 126.968815 \n",
       "L 173.8375 126.887033 \n",
       "L 170.785938 126.79814 \n",
       "L 167.734375 126.702135 \n",
       "L 164.682813 126.599019 \n",
       "L 161.63125 126.488791 \n",
       "L 158.579688 126.371452 \n",
       "L 155.528125 126.247002 \n",
       "L 152.476563 126.11544 \n",
       "L 149.425 125.976766 \n",
       "L 146.373438 125.830981 \n",
       "L 143.605741 125.692308 \n",
       "L 143.321875 125.67748 \n",
       "L 140.270313 125.510663 \n",
       "L 137.21875 125.336432 \n",
       "L 134.167188 125.154787 \n",
       "L 131.115625 124.965728 \n",
       "L 128.064063 124.769255 \n",
       "L 125.0125 124.565368 \n",
       "L 121.960938 124.354067 \n",
       "L 118.909375 124.135352 \n",
       "L 115.857813 123.909223 \n",
       "L 112.80625 123.675679 \n",
       "L 109.754688 123.434722 \n",
       "L 106.703125 123.18635 \n",
       "L 103.651563 122.930565 \n",
       "L 100.6 122.667365 \n",
       "L 97.548438 122.396751 \n",
       "L 95.473375 122.207692 \n",
       "L 94.496875 122.114769 \n",
       "L 91.445313 121.816641 \n",
       "L 88.39375 121.510769 \n",
       "L 85.342188 121.197154 \n",
       "L 82.290625 120.875795 \n",
       "L 79.239063 120.546692 \n",
       "L 76.1875 120.209846 \n",
       "L 73.135938 119.865256 \n",
       "L 70.084375 119.512923 \n",
       "L 67.032813 119.152846 \n",
       "L 63.98125 118.785026 \n",
       "L 63.477899 118.723077 \n",
       "L 60.929688 118.394875 \n",
       "L 57.878125 117.993739 \n",
       "L 54.826563 117.584499 \n",
       "L 51.775 117.167156 \n",
       "L 48.723438 116.741708 \n",
       "L 45.671875 116.308157 \n",
       "L 42.620313 115.866503 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_5\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 237.920313 137.220049 \n",
       "L 234.86875 137.273902 \n",
       "L 231.817188 137.32142 \n",
       "L 228.765625 137.362601 \n",
       "L 225.714063 137.397448 \n",
       "L 222.6625 137.425958 \n",
       "L 219.610938 137.448133 \n",
       "L 216.559375 137.463972 \n",
       "L 213.507813 137.473476 \n",
       "L 210.45625 137.476643 \n",
       "L 207.404688 137.473476 \n",
       "L 204.353125 137.463972 \n",
       "L 201.301563 137.448133 \n",
       "L 198.25 137.425958 \n",
       "L 195.198438 137.397448 \n",
       "L 192.146875 137.362601 \n",
       "L 189.095313 137.32142 \n",
       "L 186.04375 137.273902 \n",
       "L 182.992188 137.220049 \n",
       "L 179.940625 137.15986 \n",
       "L 176.889063 137.093336 \n",
       "L 173.8375 137.020476 \n",
       "L 170.785938 136.94128 \n",
       "L 167.734375 136.855748 \n",
       "L 164.682813 136.763881 \n",
       "L 161.63125 136.665678 \n",
       "L 158.579688 136.56114 \n",
       "L 155.528125 136.450266 \n",
       "L 152.476563 136.333056 \n",
       "L 149.425 136.20951 \n",
       "L 147.936433 136.146154 \n",
       "L 146.373438 136.077119 \n",
       "L 143.321875 135.935762 \n",
       "L 140.270313 135.78783 \n",
       "L 137.21875 135.633324 \n",
       "L 134.167188 135.472242 \n",
       "L 131.115625 135.304586 \n",
       "L 128.064063 135.130356 \n",
       "L 125.0125 134.94955 \n",
       "L 121.960938 134.76217 \n",
       "L 118.909375 134.568215 \n",
       "L 115.857813 134.367685 \n",
       "L 112.80625 134.160581 \n",
       "L 109.754688 133.946901 \n",
       "L 106.703125 133.726647 \n",
       "L 103.651563 133.499819 \n",
       "L 100.6 133.266415 \n",
       "L 97.548438 133.026437 \n",
       "L 94.496875 132.779884 \n",
       "L 93.07017 132.661538 \n",
       "L 91.445313 132.521471 \n",
       "L 88.39375 132.251584 \n",
       "L 85.342188 131.974864 \n",
       "L 82.290625 131.691312 \n",
       "L 79.239063 131.400928 \n",
       "L 76.1875 131.10371 \n",
       "L 73.135938 130.799661 \n",
       "L 70.084375 130.488778 \n",
       "L 67.032813 130.171063 \n",
       "L 63.98125 129.846516 \n",
       "L 60.929688 129.515136 \n",
       "L 57.878125 129.176923 \n",
       "L 57.878125 129.176923 \n",
       "L 54.826563 128.817794 \n",
       "L 51.775 128.451554 \n",
       "L 48.723438 128.078203 \n",
       "L 45.671875 127.697739 \n",
       "L 42.620313 127.310165 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_6\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 210.45625 146.6 \n",
       "L 207.404688 146.597047 \n",
       "L 204.353125 146.588188 \n",
       "L 201.301563 146.573422 \n",
       "L 198.25 146.552751 \n",
       "L 195.198438 146.526173 \n",
       "L 192.146875 146.49369 \n",
       "L 189.095313 146.4553 \n",
       "L 186.04375 146.411004 \n",
       "L 182.992188 146.360802 \n",
       "L 179.940625 146.304694 \n",
       "L 176.889063 146.242679 \n",
       "L 173.8375 146.174759 \n",
       "L 170.785938 146.100932 \n",
       "L 167.734375 146.021199 \n",
       "L 164.682813 145.935561 \n",
       "L 161.63125 145.844016 \n",
       "L 158.579688 145.746565 \n",
       "L 155.528125 145.643207 \n",
       "L 152.476563 145.533944 \n",
       "L 149.425 145.418774 \n",
       "L 146.373438 145.297699 \n",
       "L 143.321875 145.170717 \n",
       "L 140.270313 145.037829 \n",
       "L 137.21875 144.899035 \n",
       "L 134.167188 144.754335 \n",
       "L 131.115625 144.603729 \n",
       "L 128.064063 144.447216 \n",
       "L 125.0125 144.284798 \n",
       "L 121.960938 144.116473 \n",
       "L 118.909375 143.942243 \n",
       "L 115.857813 143.762106 \n",
       "L 112.80625 143.576063 \n",
       "L 109.754688 143.384113 \n",
       "L 106.703125 143.186258 \n",
       "L 105.641712 143.115385 \n",
       "L 103.651563 142.977834 \n",
       "L 100.6 142.76081 \n",
       "L 97.548438 142.537672 \n",
       "L 94.496875 142.308421 \n",
       "L 91.445313 142.073057 \n",
       "L 88.39375 141.831579 \n",
       "L 85.342188 141.583988 \n",
       "L 82.290625 141.330283 \n",
       "L 79.239063 141.070466 \n",
       "L 76.1875 140.804534 \n",
       "L 73.135938 140.53249 \n",
       "L 70.084375 140.254332 \n",
       "L 67.032813 139.970061 \n",
       "L 63.98125 139.679676 \n",
       "L 63.477899 139.630769 \n",
       "L 60.929688 139.374175 \n",
       "L 57.878125 139.060559 \n",
       "L 54.826563 138.740608 \n",
       "L 51.775 138.414322 \n",
       "L 48.723438 138.081699 \n",
       "L 45.671875 137.742741 \n",
       "L 42.620313 137.397448 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 237.920313 146.360802 \n",
       "L 234.86875 146.411004 \n",
       "L 231.817188 146.4553 \n",
       "L 228.765625 146.49369 \n",
       "L 225.714063 146.526173 \n",
       "L 222.6625 146.552751 \n",
       "L 219.610938 146.573422 \n",
       "L 216.559375 146.588188 \n",
       "L 213.507813 146.597047 \n",
       "L 210.45625 146.6 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_7\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 43.320212 146.6 \n",
       "L 42.620313 146.526173 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_8\">\n",
       "    <path clip-path=\"url(#p21b7136bd9)\" d=\"M 57.878125 111.753846 \n",
       "L 70.084369 97.815385 \n",
       "L 78.348688 89.108097 \n",
       "L 84.908015 82.608408 \n",
       "L 90.459611 77.410729 \n",
       "L 95.325825 73.101347 \n",
       "L 99.686585 69.4495 \n",
       "L 103.654327 66.310089 \n",
       "L 107.304852 63.584212 \n",
       "L 110.692126 61.200407 \n",
       "L 113.856191 59.104725 \n",
       "L 116.827733 57.255022 \n",
       "L 119.630892 55.617488 \n",
       "L 122.285062 54.164405 \n",
       "L 124.806115 52.872664 \n",
       "L 127.207235 51.722725 \n",
       "L 129.499519 50.697881 \n",
       "L 131.692415 49.783724 \n",
       "L 133.79405 48.967733 \n",
       "L 135.811475 48.238964 \n",
       "L 137.750858 47.587809 \n",
       "\" style=\"fill:none;stroke:#ff7f0e;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "    <defs>\n",
       "     <path d=\"M 0 3 \n",
       "C 0.795609 3 1.55874 2.683901 2.12132 2.12132 \n",
       "C 2.683901 1.55874 3 0.795609 3 0 \n",
       "C 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \n",
       "C 1.55874 -2.683901 0.795609 -3 0 -3 \n",
       "C -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \n",
       "C -2.683901 -1.55874 -3 -0.795609 -3 0 \n",
       "C -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \n",
       "C -1.55874 2.683901 -0.795609 3 0 3 \n",
       "z\n",
       "\" id=\"m2a9f8bf048\" style=\"stroke:#ff7f0e;\"/>\n",
       "    </defs>\n",
       "    <g clip-path=\"url(#p21b7136bd9)\">\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"57.878125\" xlink:href=\"#m2a9f8bf048\" y=\"111.753846\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"70.084369\" xlink:href=\"#m2a9f8bf048\" y=\"97.815385\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"78.348688\" xlink:href=\"#m2a9f8bf048\" y=\"89.108097\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"84.908015\" xlink:href=\"#m2a9f8bf048\" y=\"82.608408\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"90.459611\" xlink:href=\"#m2a9f8bf048\" y=\"77.410729\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"95.325825\" xlink:href=\"#m2a9f8bf048\" y=\"73.101347\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"99.686585\" xlink:href=\"#m2a9f8bf048\" y=\"69.4495\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"103.654327\" xlink:href=\"#m2a9f8bf048\" y=\"66.310089\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"107.304852\" xlink:href=\"#m2a9f8bf048\" y=\"63.584212\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"110.692126\" xlink:href=\"#m2a9f8bf048\" y=\"61.200407\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"113.856191\" xlink:href=\"#m2a9f8bf048\" y=\"59.104725\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"116.827733\" xlink:href=\"#m2a9f8bf048\" y=\"57.255022\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"119.630892\" xlink:href=\"#m2a9f8bf048\" y=\"55.617488\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"122.285062\" xlink:href=\"#m2a9f8bf048\" y=\"54.164405\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"124.806115\" xlink:href=\"#m2a9f8bf048\" y=\"52.872664\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"127.207235\" xlink:href=\"#m2a9f8bf048\" y=\"51.722725\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"129.499519\" xlink:href=\"#m2a9f8bf048\" y=\"50.697881\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"131.692415\" xlink:href=\"#m2a9f8bf048\" y=\"49.783724\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"133.79405\" xlink:href=\"#m2a9f8bf048\" y=\"48.967733\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"135.811475\" xlink:href=\"#m2a9f8bf048\" y=\"48.238964\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"137.750858\" xlink:href=\"#m2a9f8bf048\" y=\"47.587809\"/>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 42.620312 146.6 \n",
       "L 42.620312 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 237.920313 146.6 \n",
       "L 237.920313 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 42.620313 146.6 \n",
       "L 237.920313 146.6 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 42.620313 10.7 \n",
       "L 237.920313 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p21b7136bd9\">\n",
       "   <rect height=\"135.9\" width=\"195.3\" x=\"42.620312\" y=\"10.7\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 252x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import d2lzh as d2l\n",
    "import math\n",
    "from mxnet import nd\n",
    "\n",
    "def adagrad_2d(x1, x2, s1, s2):\n",
    "    g1, g2, eps = 0.2 * x1, 4 * x2, 1e-6  # 前两项为自变量梯度\n",
    "    s1 += g1 ** 2\n",
    "    s2 += g2 ** 2\n",
    "    x1 -= eta / math.sqrt(s1 + eps) * g1\n",
    "    x2 -= eta / math.sqrt(s2 + eps) * g2\n",
    "    return x1, x2, s1, s2\n",
    "\n",
    "def f_2d(x1, x2):\n",
    "    return 0.1 * x1 ** 2 + 2 * x2 ** 2\n",
    "\n",
    "eta = 0.4\n",
    "d2l.show_trace_2d(f_2d, d2l.train_2d(adagrad_2d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面将学习率增大到2。可以看到自变量更为迅速地逼近了最优解。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "3"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 20, x1 -0.002295, x2 -0.000000\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Created with matplotlib (http://matplotlib.org/) -->\n",
       "<svg height=\"184.15625pt\" version=\"1.1\" viewBox=\"0 0 248.620313 184.15625\" width=\"248.620313pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <defs>\n",
       "  <style type=\"text/css\">\n",
       "*{stroke-linecap:butt;stroke-linejoin:round;}\n",
       "  </style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 184.15625 \n",
       "L 248.620313 184.15625 \n",
       "L 248.620313 -0 \n",
       "L 0 -0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 42.620312 146.6 \n",
       "L 237.920313 146.6 \n",
       "L 237.920313 10.7 \n",
       "L 42.620312 10.7 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"m04af57c94e\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"88.39375\" xlink:href=\"#m04af57c94e\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- −4 -->\n",
       "      <defs>\n",
       "       <path d=\"M 10.59375 35.5 \n",
       "L 73.1875 35.5 \n",
       "L 73.1875 27.203125 \n",
       "L 10.59375 27.203125 \n",
       "z\n",
       "\" id=\"DejaVuSans-2212\"/>\n",
       "       <path d=\"M 37.796875 64.3125 \n",
       "L 12.890625 25.390625 \n",
       "L 37.796875 25.390625 \n",
       "z\n",
       "M 35.203125 72.90625 \n",
       "L 47.609375 72.90625 \n",
       "L 47.609375 25.390625 \n",
       "L 58.015625 25.390625 \n",
       "L 58.015625 17.1875 \n",
       "L 47.609375 17.1875 \n",
       "L 47.609375 0 \n",
       "L 37.796875 0 \n",
       "L 37.796875 17.1875 \n",
       "L 4.890625 17.1875 \n",
       "L 4.890625 26.703125 \n",
       "z\n",
       "\" id=\"DejaVuSans-34\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(81.022656 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"149.425\" xlink:href=\"#m04af57c94e\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- −2 -->\n",
       "      <defs>\n",
       "       <path d=\"M 19.1875 8.296875 \n",
       "L 53.609375 8.296875 \n",
       "L 53.609375 0 \n",
       "L 7.328125 0 \n",
       "L 7.328125 8.296875 \n",
       "Q 12.9375 14.109375 22.625 23.890625 \n",
       "Q 32.328125 33.6875 34.8125 36.53125 \n",
       "Q 39.546875 41.84375 41.421875 45.53125 \n",
       "Q 43.3125 49.21875 43.3125 52.78125 \n",
       "Q 43.3125 58.59375 39.234375 62.25 \n",
       "Q 35.15625 65.921875 28.609375 65.921875 \n",
       "Q 23.96875 65.921875 18.8125 64.3125 \n",
       "Q 13.671875 62.703125 7.8125 59.421875 \n",
       "L 7.8125 69.390625 \n",
       "Q 13.765625 71.78125 18.9375 73 \n",
       "Q 24.125 74.21875 28.421875 74.21875 \n",
       "Q 39.75 74.21875 46.484375 68.546875 \n",
       "Q 53.21875 62.890625 53.21875 53.421875 \n",
       "Q 53.21875 48.921875 51.53125 44.890625 \n",
       "Q 49.859375 40.875 45.40625 35.40625 \n",
       "Q 44.1875 33.984375 37.640625 27.21875 \n",
       "Q 31.109375 20.453125 19.1875 8.296875 \n",
       "z\n",
       "\" id=\"DejaVuSans-32\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(142.053906 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"210.45625\" xlink:href=\"#m04af57c94e\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 31.78125 66.40625 \n",
       "Q 24.171875 66.40625 20.328125 58.90625 \n",
       "Q 16.5 51.421875 16.5 36.375 \n",
       "Q 16.5 21.390625 20.328125 13.890625 \n",
       "Q 24.171875 6.390625 31.78125 6.390625 \n",
       "Q 39.453125 6.390625 43.28125 13.890625 \n",
       "Q 47.125 21.390625 47.125 36.375 \n",
       "Q 47.125 51.421875 43.28125 58.90625 \n",
       "Q 39.453125 66.40625 31.78125 66.40625 \n",
       "z\n",
       "M 31.78125 74.21875 \n",
       "Q 44.046875 74.21875 50.515625 64.515625 \n",
       "Q 56.984375 54.828125 56.984375 36.375 \n",
       "Q 56.984375 17.96875 50.515625 8.265625 \n",
       "Q 44.046875 -1.421875 31.78125 -1.421875 \n",
       "Q 19.53125 -1.421875 13.0625 8.265625 \n",
       "Q 6.59375 17.96875 6.59375 36.375 \n",
       "Q 6.59375 54.828125 13.0625 64.515625 \n",
       "Q 19.53125 74.21875 31.78125 74.21875 \n",
       "z\n",
       "\" id=\"DejaVuSans-30\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(207.275 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_4\">\n",
       "     <!-- x1 -->\n",
       "     <defs>\n",
       "      <path d=\"M 54.890625 54.6875 \n",
       "L 35.109375 28.078125 \n",
       "L 55.90625 0 \n",
       "L 45.3125 0 \n",
       "L 29.390625 21.484375 \n",
       "L 13.484375 0 \n",
       "L 2.875 0 \n",
       "L 24.125 28.609375 \n",
       "L 4.6875 54.6875 \n",
       "L 15.28125 54.6875 \n",
       "L 29.78125 35.203125 \n",
       "L 44.28125 54.6875 \n",
       "z\n",
       "\" id=\"DejaVuSans-78\"/>\n",
       "      <path d=\"M 12.40625 8.296875 \n",
       "L 28.515625 8.296875 \n",
       "L 28.515625 63.921875 \n",
       "L 10.984375 60.40625 \n",
       "L 10.984375 69.390625 \n",
       "L 28.421875 72.90625 \n",
       "L 38.28125 72.90625 \n",
       "L 38.28125 8.296875 \n",
       "L 54.390625 8.296875 \n",
       "L 54.390625 0 \n",
       "L 12.40625 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-31\"/>\n",
       "     </defs>\n",
       "     <g transform=\"translate(134.129687 174.876562)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"59.179688\" xlink:href=\"#DejaVuSans-31\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m183072d520\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m183072d520\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- −3 -->\n",
       "      <defs>\n",
       "       <path d=\"M 40.578125 39.3125 \n",
       "Q 47.65625 37.796875 51.625 33 \n",
       "Q 55.609375 28.21875 55.609375 21.1875 \n",
       "Q 55.609375 10.40625 48.1875 4.484375 \n",
       "Q 40.765625 -1.421875 27.09375 -1.421875 \n",
       "Q 22.515625 -1.421875 17.65625 -0.515625 \n",
       "Q 12.796875 0.390625 7.625 2.203125 \n",
       "L 7.625 11.71875 \n",
       "Q 11.71875 9.328125 16.59375 8.109375 \n",
       "Q 21.484375 6.890625 26.8125 6.890625 \n",
       "Q 36.078125 6.890625 40.9375 10.546875 \n",
       "Q 45.796875 14.203125 45.796875 21.1875 \n",
       "Q 45.796875 27.640625 41.28125 31.265625 \n",
       "Q 36.765625 34.90625 28.71875 34.90625 \n",
       "L 20.21875 34.90625 \n",
       "L 20.21875 43.015625 \n",
       "L 29.109375 43.015625 \n",
       "Q 36.375 43.015625 40.234375 45.921875 \n",
       "Q 44.09375 48.828125 44.09375 54.296875 \n",
       "Q 44.09375 59.90625 40.109375 62.90625 \n",
       "Q 36.140625 65.921875 28.71875 65.921875 \n",
       "Q 24.65625 65.921875 20.015625 65.03125 \n",
       "Q 15.375 64.15625 9.8125 62.3125 \n",
       "L 9.8125 71.09375 \n",
       "Q 15.4375 72.65625 20.34375 73.4375 \n",
       "Q 25.25 74.21875 29.59375 74.21875 \n",
       "Q 40.828125 74.21875 47.359375 69.109375 \n",
       "Q 53.90625 64.015625 53.90625 55.328125 \n",
       "Q 53.90625 49.265625 50.4375 45.09375 \n",
       "Q 46.96875 40.921875 40.578125 39.3125 \n",
       "z\n",
       "\" id=\"DejaVuSans-33\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(20.878125 150.399219)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-33\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m183072d520\" y=\"111.753846\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <!-- −2 -->\n",
       "      <g transform=\"translate(20.878125 115.553065)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m183072d520\" y=\"76.907692\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- −1 -->\n",
       "      <g transform=\"translate(20.878125 80.706911)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-31\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"42.620312\" xlink:href=\"#m183072d520\" y=\"42.061538\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(29.257812 45.860757)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_9\">\n",
       "     <!-- x2 -->\n",
       "     <g transform=\"translate(14.798437 84.790625)rotate(-90)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"59.179688\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_1\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 237.920313 84.148723 \n",
       "L 234.86875 84.2672 \n",
       "L 231.817188 84.371738 \n",
       "L 228.765625 84.462338 \n",
       "L 225.714063 84.539 \n",
       "L 222.6625 84.601723 \n",
       "L 219.610938 84.650508 \n",
       "L 216.559375 84.685354 \n",
       "L 213.507813 84.706262 \n",
       "L 210.45625 84.713231 \n",
       "L 207.404688 84.706262 \n",
       "L 204.353125 84.685354 \n",
       "L 201.301563 84.650508 \n",
       "L 198.25 84.601723 \n",
       "L 195.198438 84.539 \n",
       "L 192.146875 84.462338 \n",
       "L 189.095313 84.371738 \n",
       "L 186.04375 84.2672 \n",
       "L 182.992188 84.148723 \n",
       "L 179.940625 84.016308 \n",
       "L 177.034375 83.876923 \n",
       "L 176.889063 83.869348 \n",
       "L 173.8375 83.695117 \n",
       "L 170.785938 83.505736 \n",
       "L 167.734375 83.301204 \n",
       "L 164.682813 83.081522 \n",
       "L 161.63125 82.846689 \n",
       "L 158.579688 82.596706 \n",
       "L 155.528125 82.331572 \n",
       "L 152.476563 82.051288 \n",
       "L 149.425 81.755853 \n",
       "L 146.373438 81.445268 \n",
       "L 143.321875 81.119532 \n",
       "L 140.270313 80.778645 \n",
       "L 137.21875 80.422609 \n",
       "L 136.969643 80.392308 \n",
       "L 134.167188 80.018956 \n",
       "L 131.115625 79.595824 \n",
       "L 128.064063 79.156099 \n",
       "L 125.0125 78.69978 \n",
       "L 121.960938 78.226868 \n",
       "L 118.909375 77.737363 \n",
       "L 115.857813 77.231264 \n",
       "L 113.96875 76.907692 \n",
       "L 112.80625 76.687611 \n",
       "L 109.754688 76.091559 \n",
       "L 106.703125 75.477166 \n",
       "L 103.651563 74.844433 \n",
       "L 100.6 74.19336 \n",
       "L 97.548438 73.523947 \n",
       "L 97.100875 73.423077 \n",
       "L 94.496875 72.767149 \n",
       "L 91.445313 71.977986 \n",
       "L 88.39375 71.168326 \n",
       "L 85.342188 70.338167 \n",
       "L 83.908321 69.938462 \n",
       "L 82.290625 69.427385 \n",
       "L 79.239063 68.440077 \n",
       "L 76.1875 67.429538 \n",
       "L 73.307374 66.453846 \n",
       "L 73.135938 66.386834 \n",
       "L 70.084375 65.167219 \n",
       "L 67.032813 63.920799 \n",
       "L 64.752171 62.969231 \n",
       "L 63.98125 62.589091 \n",
       "L 60.929688 61.052692 \n",
       "L 57.878125 59.484615 \n",
       "L 57.878125 59.484615 \n",
       "L 54.826563 57.529359 \n",
       "L 52.486044 56 \n",
       "L 51.775 55.402637 \n",
       "L 48.723438 52.789176 \n",
       "L 48.409725 52.515385 \n",
       "L 45.671875 49.170154 \n",
       "L 45.559891 49.030769 \n",
       "L 43.880132 45.546154 \n",
       "L 43.320212 42.061538 \n",
       "L 43.880132 38.576923 \n",
       "L 45.559891 35.092308 \n",
       "L 45.671875 34.952923 \n",
       "L 48.409725 31.607692 \n",
       "L 48.723438 31.333901 \n",
       "L 51.775 28.72044 \n",
       "L 52.486044 28.123077 \n",
       "L 54.826563 26.593718 \n",
       "L 57.878125 24.638462 \n",
       "L 57.878125 24.638462 \n",
       "L 60.929688 23.070385 \n",
       "L 63.98125 21.533986 \n",
       "L 64.752171 21.153846 \n",
       "L 67.032813 20.202278 \n",
       "L 70.084375 18.955858 \n",
       "L 73.135938 17.736243 \n",
       "L 73.307374 17.669231 \n",
       "L 76.1875 16.693538 \n",
       "L 79.239063 15.683 \n",
       "L 82.290625 14.695692 \n",
       "L 83.908321 14.184615 \n",
       "L 85.342188 13.78491 \n",
       "L 88.39375 12.954751 \n",
       "L 91.445313 12.14509 \n",
       "L 94.496875 11.355928 \n",
       "L 97.100875 10.7 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_2\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 237.920313 101.991945 \n",
       "L 234.86875 102.076571 \n",
       "L 231.817188 102.151242 \n",
       "L 228.765625 102.215956 \n",
       "L 225.714063 102.270714 \n",
       "L 222.6625 102.315516 \n",
       "L 219.610938 102.350363 \n",
       "L 216.559375 102.375253 \n",
       "L 213.507813 102.390187 \n",
       "L 210.45625 102.395165 \n",
       "L 207.404688 102.390187 \n",
       "L 204.353125 102.375253 \n",
       "L 201.301563 102.350363 \n",
       "L 198.25 102.315516 \n",
       "L 195.198438 102.270714 \n",
       "L 192.146875 102.215956 \n",
       "L 189.095313 102.151242 \n",
       "L 186.04375 102.076571 \n",
       "L 182.992188 101.991945 \n",
       "L 179.940625 101.897363 \n",
       "L 176.889063 101.792824 \n",
       "L 173.8375 101.67833 \n",
       "L 170.785938 101.553879 \n",
       "L 167.734375 101.419473 \n",
       "L 165.208944 101.3 \n",
       "L 164.682813 101.273601 \n",
       "L 161.63125 101.10993 \n",
       "L 158.579688 100.935699 \n",
       "L 155.528125 100.750909 \n",
       "L 152.476563 100.555559 \n",
       "L 149.425 100.34965 \n",
       "L 146.373438 100.133182 \n",
       "L 143.321875 99.906154 \n",
       "L 140.270313 99.668566 \n",
       "L 137.21875 99.42042 \n",
       "L 134.167188 99.161713 \n",
       "L 131.115625 98.892448 \n",
       "L 128.064063 98.612622 \n",
       "L 125.0125 98.322238 \n",
       "L 121.960938 98.021294 \n",
       "L 119.943803 97.815385 \n",
       "L 118.909375 97.702978 \n",
       "L 115.857813 97.360136 \n",
       "L 112.80625 97.006055 \n",
       "L 109.754688 96.640732 \n",
       "L 106.703125 96.264169 \n",
       "L 103.651563 95.876365 \n",
       "L 100.6 95.47732 \n",
       "L 97.548438 95.067035 \n",
       "L 94.496875 94.645509 \n",
       "L 92.277557 94.330769 \n",
       "L 91.445313 94.204602 \n",
       "L 88.39375 93.729973 \n",
       "L 85.342188 93.243329 \n",
       "L 82.290625 92.744668 \n",
       "L 79.239063 92.233992 \n",
       "L 76.1875 91.7113 \n",
       "L 73.135938 91.176592 \n",
       "L 71.291587 90.846154 \n",
       "L 70.084375 90.613846 \n",
       "L 67.032813 90.013718 \n",
       "L 63.98125 89.400684 \n",
       "L 60.929688 88.774744 \n",
       "L 57.878125 88.135897 \n",
       "L 54.826563 87.484145 \n",
       "L 54.263653 87.361538 \n",
       "L 51.775 86.776123 \n",
       "L 48.723438 86.044354 \n",
       "L 45.671875 85.298646 \n",
       "L 42.620313 84.539 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_3\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 237.920313 115.639597 \n",
       "L 234.86875 115.708479 \n",
       "L 231.817188 115.769258 \n",
       "L 228.765625 115.821932 \n",
       "L 225.714063 115.866503 \n",
       "L 222.6625 115.90297 \n",
       "L 219.610938 115.931333 \n",
       "L 216.559375 115.951592 \n",
       "L 213.507813 115.963748 \n",
       "L 210.45625 115.9678 \n",
       "L 207.404688 115.963748 \n",
       "L 204.353125 115.951592 \n",
       "L 201.301563 115.931333 \n",
       "L 198.25 115.90297 \n",
       "L 195.198438 115.866503 \n",
       "L 192.146875 115.821932 \n",
       "L 189.095313 115.769258 \n",
       "L 186.04375 115.708479 \n",
       "L 182.992188 115.639597 \n",
       "L 179.940625 115.562612 \n",
       "L 176.889063 115.477522 \n",
       "L 173.8375 115.384329 \n",
       "L 170.785938 115.283032 \n",
       "L 169.542708 115.238462 \n",
       "L 167.734375 115.170469 \n",
       "L 164.682813 115.047233 \n",
       "L 161.63125 114.915497 \n",
       "L 158.579688 114.775263 \n",
       "L 155.528125 114.626529 \n",
       "L 152.476563 114.469296 \n",
       "L 149.425 114.303565 \n",
       "L 146.373438 114.129334 \n",
       "L 143.321875 113.946604 \n",
       "L 140.270313 113.755375 \n",
       "L 137.21875 113.555647 \n",
       "L 134.167188 113.34742 \n",
       "L 131.115625 113.130694 \n",
       "L 128.064063 112.905469 \n",
       "L 125.0125 112.671745 \n",
       "L 121.960938 112.429522 \n",
       "L 118.909375 112.178799 \n",
       "L 115.857813 111.919578 \n",
       "L 113.96875 111.753846 \n",
       "L 112.80625 111.646627 \n",
       "L 109.754688 111.356243 \n",
       "L 106.703125 111.056923 \n",
       "L 103.651563 110.748669 \n",
       "L 100.6 110.431479 \n",
       "L 97.548438 110.105355 \n",
       "L 94.496875 109.770296 \n",
       "L 91.445313 109.426302 \n",
       "L 88.39375 109.073373 \n",
       "L 85.342188 108.711509 \n",
       "L 82.290625 108.34071 \n",
       "L 81.716213 108.269231 \n",
       "L 79.239063 107.944314 \n",
       "L 76.1875 107.534636 \n",
       "L 73.135938 107.115541 \n",
       "L 70.084375 106.687027 \n",
       "L 67.032813 106.249096 \n",
       "L 63.98125 105.801746 \n",
       "L 60.929688 105.344979 \n",
       "L 57.878125 104.878794 \n",
       "L 57.273855 104.784615 \n",
       "L 54.826563 104.381396 \n",
       "L 51.775 103.868659 \n",
       "L 48.723438 103.345967 \n",
       "L 45.671875 102.813319 \n",
       "L 42.620313 102.270714 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_4\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 237.920313 127.111044 \n",
       "L 234.86875 127.171491 \n",
       "L 231.817188 127.224827 \n",
       "L 228.765625 127.271052 \n",
       "L 225.714063 127.310165 \n",
       "L 222.6625 127.342166 \n",
       "L 219.610938 127.367057 \n",
       "L 216.559375 127.384835 \n",
       "L 213.507813 127.395502 \n",
       "L 210.45625 127.399058 \n",
       "L 207.404688 127.395502 \n",
       "L 204.353125 127.384835 \n",
       "L 201.301563 127.367057 \n",
       "L 198.25 127.342166 \n",
       "L 195.198438 127.310165 \n",
       "L 192.146875 127.271052 \n",
       "L 189.095313 127.224827 \n",
       "L 186.04375 127.171491 \n",
       "L 182.992188 127.111044 \n",
       "L 179.940625 127.043485 \n",
       "L 176.889063 126.968815 \n",
       "L 173.8375 126.887033 \n",
       "L 170.785938 126.79814 \n",
       "L 167.734375 126.702135 \n",
       "L 164.682813 126.599019 \n",
       "L 161.63125 126.488791 \n",
       "L 158.579688 126.371452 \n",
       "L 155.528125 126.247002 \n",
       "L 152.476563 126.11544 \n",
       "L 149.425 125.976766 \n",
       "L 146.373438 125.830981 \n",
       "L 143.605741 125.692308 \n",
       "L 143.321875 125.67748 \n",
       "L 140.270313 125.510663 \n",
       "L 137.21875 125.336432 \n",
       "L 134.167188 125.154787 \n",
       "L 131.115625 124.965728 \n",
       "L 128.064063 124.769255 \n",
       "L 125.0125 124.565368 \n",
       "L 121.960938 124.354067 \n",
       "L 118.909375 124.135352 \n",
       "L 115.857813 123.909223 \n",
       "L 112.80625 123.675679 \n",
       "L 109.754688 123.434722 \n",
       "L 106.703125 123.18635 \n",
       "L 103.651563 122.930565 \n",
       "L 100.6 122.667365 \n",
       "L 97.548438 122.396751 \n",
       "L 95.473375 122.207692 \n",
       "L 94.496875 122.114769 \n",
       "L 91.445313 121.816641 \n",
       "L 88.39375 121.510769 \n",
       "L 85.342188 121.197154 \n",
       "L 82.290625 120.875795 \n",
       "L 79.239063 120.546692 \n",
       "L 76.1875 120.209846 \n",
       "L 73.135938 119.865256 \n",
       "L 70.084375 119.512923 \n",
       "L 67.032813 119.152846 \n",
       "L 63.98125 118.785026 \n",
       "L 63.477899 118.723077 \n",
       "L 60.929688 118.394875 \n",
       "L 57.878125 117.993739 \n",
       "L 54.826563 117.584499 \n",
       "L 51.775 117.167156 \n",
       "L 48.723438 116.741708 \n",
       "L 45.671875 116.308157 \n",
       "L 42.620313 115.866503 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_5\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 237.920313 137.220049 \n",
       "L 234.86875 137.273902 \n",
       "L 231.817188 137.32142 \n",
       "L 228.765625 137.362601 \n",
       "L 225.714063 137.397448 \n",
       "L 222.6625 137.425958 \n",
       "L 219.610938 137.448133 \n",
       "L 216.559375 137.463972 \n",
       "L 213.507813 137.473476 \n",
       "L 210.45625 137.476643 \n",
       "L 207.404688 137.473476 \n",
       "L 204.353125 137.463972 \n",
       "L 201.301563 137.448133 \n",
       "L 198.25 137.425958 \n",
       "L 195.198438 137.397448 \n",
       "L 192.146875 137.362601 \n",
       "L 189.095313 137.32142 \n",
       "L 186.04375 137.273902 \n",
       "L 182.992188 137.220049 \n",
       "L 179.940625 137.15986 \n",
       "L 176.889063 137.093336 \n",
       "L 173.8375 137.020476 \n",
       "L 170.785938 136.94128 \n",
       "L 167.734375 136.855748 \n",
       "L 164.682813 136.763881 \n",
       "L 161.63125 136.665678 \n",
       "L 158.579688 136.56114 \n",
       "L 155.528125 136.450266 \n",
       "L 152.476563 136.333056 \n",
       "L 149.425 136.20951 \n",
       "L 147.936433 136.146154 \n",
       "L 146.373438 136.077119 \n",
       "L 143.321875 135.935762 \n",
       "L 140.270313 135.78783 \n",
       "L 137.21875 135.633324 \n",
       "L 134.167188 135.472242 \n",
       "L 131.115625 135.304586 \n",
       "L 128.064063 135.130356 \n",
       "L 125.0125 134.94955 \n",
       "L 121.960938 134.76217 \n",
       "L 118.909375 134.568215 \n",
       "L 115.857813 134.367685 \n",
       "L 112.80625 134.160581 \n",
       "L 109.754688 133.946901 \n",
       "L 106.703125 133.726647 \n",
       "L 103.651563 133.499819 \n",
       "L 100.6 133.266415 \n",
       "L 97.548438 133.026437 \n",
       "L 94.496875 132.779884 \n",
       "L 93.07017 132.661538 \n",
       "L 91.445313 132.521471 \n",
       "L 88.39375 132.251584 \n",
       "L 85.342188 131.974864 \n",
       "L 82.290625 131.691312 \n",
       "L 79.239063 131.400928 \n",
       "L 76.1875 131.10371 \n",
       "L 73.135938 130.799661 \n",
       "L 70.084375 130.488778 \n",
       "L 67.032813 130.171063 \n",
       "L 63.98125 129.846516 \n",
       "L 60.929688 129.515136 \n",
       "L 57.878125 129.176923 \n",
       "L 57.878125 129.176923 \n",
       "L 54.826563 128.817794 \n",
       "L 51.775 128.451554 \n",
       "L 48.723438 128.078203 \n",
       "L 45.671875 127.697739 \n",
       "L 42.620313 127.310165 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_6\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 210.45625 146.6 \n",
       "L 207.404688 146.597047 \n",
       "L 204.353125 146.588188 \n",
       "L 201.301563 146.573422 \n",
       "L 198.25 146.552751 \n",
       "L 195.198438 146.526173 \n",
       "L 192.146875 146.49369 \n",
       "L 189.095313 146.4553 \n",
       "L 186.04375 146.411004 \n",
       "L 182.992188 146.360802 \n",
       "L 179.940625 146.304694 \n",
       "L 176.889063 146.242679 \n",
       "L 173.8375 146.174759 \n",
       "L 170.785938 146.100932 \n",
       "L 167.734375 146.021199 \n",
       "L 164.682813 145.935561 \n",
       "L 161.63125 145.844016 \n",
       "L 158.579688 145.746565 \n",
       "L 155.528125 145.643207 \n",
       "L 152.476563 145.533944 \n",
       "L 149.425 145.418774 \n",
       "L 146.373438 145.297699 \n",
       "L 143.321875 145.170717 \n",
       "L 140.270313 145.037829 \n",
       "L 137.21875 144.899035 \n",
       "L 134.167188 144.754335 \n",
       "L 131.115625 144.603729 \n",
       "L 128.064063 144.447216 \n",
       "L 125.0125 144.284798 \n",
       "L 121.960938 144.116473 \n",
       "L 118.909375 143.942243 \n",
       "L 115.857813 143.762106 \n",
       "L 112.80625 143.576063 \n",
       "L 109.754688 143.384113 \n",
       "L 106.703125 143.186258 \n",
       "L 105.641712 143.115385 \n",
       "L 103.651563 142.977834 \n",
       "L 100.6 142.76081 \n",
       "L 97.548438 142.537672 \n",
       "L 94.496875 142.308421 \n",
       "L 91.445313 142.073057 \n",
       "L 88.39375 141.831579 \n",
       "L 85.342188 141.583988 \n",
       "L 82.290625 141.330283 \n",
       "L 79.239063 141.070466 \n",
       "L 76.1875 140.804534 \n",
       "L 73.135938 140.53249 \n",
       "L 70.084375 140.254332 \n",
       "L 67.032813 139.970061 \n",
       "L 63.98125 139.679676 \n",
       "L 63.477899 139.630769 \n",
       "L 60.929688 139.374175 \n",
       "L 57.878125 139.060559 \n",
       "L 54.826563 138.740608 \n",
       "L 51.775 138.414322 \n",
       "L 48.723438 138.081699 \n",
       "L 45.671875 137.742741 \n",
       "L 42.620313 137.397448 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 237.920313 146.360802 \n",
       "L 234.86875 146.411004 \n",
       "L 231.817188 146.4553 \n",
       "L 228.765625 146.49369 \n",
       "L 225.714063 146.526173 \n",
       "L 222.6625 146.552751 \n",
       "L 219.610938 146.573422 \n",
       "L 216.559375 146.588188 \n",
       "L 213.507813 146.597047 \n",
       "L 210.45625 146.6 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"LineCollection_7\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 43.320212 146.6 \n",
       "L 42.620313 146.526173 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_8\">\n",
       "    <path clip-path=\"url(#p0eb5765c63)\" d=\"M 57.878125 111.753846 \n",
       "L 118.909344 42.061539 \n",
       "L 150.30966 42.061538 \n",
       "L 169.853409 42.061538 \n",
       "L 182.748838 42.061538 \n",
       "L 191.458619 42.061538 \n",
       "L 197.402109 42.061538 \n",
       "L 201.477039 42.061538 \n",
       "L 204.277004 42.061538 \n",
       "L 206.202903 42.061538 \n",
       "L 207.52824 42.061538 \n",
       "L 208.440502 42.061538 \n",
       "L 209.068503 42.061538 \n",
       "L 209.500841 42.061538 \n",
       "L 209.798485 42.061538 \n",
       "L 210.003401 42.061538 \n",
       "L 210.144479 42.061538 \n",
       "L 210.241606 42.061538 \n",
       "L 210.308474 42.061538 \n",
       "L 210.354511 42.061538 \n",
       "L 210.386206 42.061538 \n",
       "\" style=\"fill:none;stroke:#ff7f0e;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "    <defs>\n",
       "     <path d=\"M 0 3 \n",
       "C 0.795609 3 1.55874 2.683901 2.12132 2.12132 \n",
       "C 2.683901 1.55874 3 0.795609 3 0 \n",
       "C 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \n",
       "C 1.55874 -2.683901 0.795609 -3 0 -3 \n",
       "C -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \n",
       "C -2.683901 -1.55874 -3 -0.795609 -3 0 \n",
       "C -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \n",
       "C -1.55874 2.683901 -0.795609 3 0 3 \n",
       "z\n",
       "\" id=\"m8aff588a09\" style=\"stroke:#ff7f0e;\"/>\n",
       "    </defs>\n",
       "    <g clip-path=\"url(#p0eb5765c63)\">\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"57.878125\" xlink:href=\"#m8aff588a09\" y=\"111.753846\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"118.909344\" xlink:href=\"#m8aff588a09\" y=\"42.061539\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"150.30966\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"169.853409\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"182.748838\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"191.458619\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"197.402109\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"201.477039\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"204.277004\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"206.202903\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"207.52824\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"208.440502\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"209.068503\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"209.500841\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"209.798485\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"210.003401\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"210.144479\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"210.241606\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"210.308474\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"210.354511\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "     <use style=\"fill:#ff7f0e;stroke:#ff7f0e;\" x=\"210.386206\" xlink:href=\"#m8aff588a09\" y=\"42.061538\"/>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 42.620312 146.6 \n",
       "L 42.620312 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 237.920313 146.6 \n",
       "L 237.920313 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 42.620313 146.6 \n",
       "L 237.920313 146.6 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 42.620313 10.7 \n",
       "L 237.920313 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p0eb5765c63\">\n",
       "   <rect height=\"135.9\" width=\"195.3\" x=\"42.620312\" y=\"10.7\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 252x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "eta = 2\n",
    "d2l.show_trace_2d(f_2d, d2l.train_2d(adagrad_2d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 从零开始实现\n",
    "\n",
    "同动量法一样，AdaGrad算法需要对每个自变量维护同它一样形状的状态变量。我们根据AdaGrad算法中的公式实现该算法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "4"
    }
   },
   "outputs": [],
   "source": [
    "features, labels = d2l.get_data_ch7()\n",
    "\n",
    "def init_adagrad_states():\n",
    "    s_w = nd.zeros((features.shape[1], 1))\n",
    "    s_b = nd.zeros(1)\n",
    "    return (s_w, s_b)\n",
    "\n",
    "def adagrad(params, states, hyperparams):\n",
    "    eps = 1e-6\n",
    "    for p, s in zip(params, states):\n",
    "        s[:] += p.grad.square()\n",
    "        p[:] -= hyperparams['lr'] * p.grad / (s + eps).sqrt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与[“小批量随机梯度下降”](minibatch-sgd.ipynb)一节中的实验相比，这里使用更大的学习率来训练模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "5"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loss: 0.243594, 0.334435 sec per epoch\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Created with matplotlib (http://matplotlib.org/) -->\n",
       "<svg height=\"184.15625pt\" version=\"1.1\" viewBox=\"0 0 249.78125 184.15625\" width=\"249.78125pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <defs>\n",
       "  <style type=\"text/css\">\n",
       "*{stroke-linecap:butt;stroke-linejoin:round;}\n",
       "  </style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 184.15625 \n",
       "L 249.78125 184.15625 \n",
       "L 249.78125 -0 \n",
       "L 0 -0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 43.78125 146.6 \n",
       "L 239.08125 146.6 \n",
       "L 239.08125 10.7 \n",
       "L 43.78125 10.7 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"md8a65da662\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"52.658523\" xlink:href=\"#md8a65da662\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- 0.0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 31.78125 66.40625 \n",
       "Q 24.171875 66.40625 20.328125 58.90625 \n",
       "Q 16.5 51.421875 16.5 36.375 \n",
       "Q 16.5 21.390625 20.328125 13.890625 \n",
       "Q 24.171875 6.390625 31.78125 6.390625 \n",
       "Q 39.453125 6.390625 43.28125 13.890625 \n",
       "Q 47.125 21.390625 47.125 36.375 \n",
       "Q 47.125 51.421875 43.28125 58.90625 \n",
       "Q 39.453125 66.40625 31.78125 66.40625 \n",
       "z\n",
       "M 31.78125 74.21875 \n",
       "Q 44.046875 74.21875 50.515625 64.515625 \n",
       "Q 56.984375 54.828125 56.984375 36.375 \n",
       "Q 56.984375 17.96875 50.515625 8.265625 \n",
       "Q 44.046875 -1.421875 31.78125 -1.421875 \n",
       "Q 19.53125 -1.421875 13.0625 8.265625 \n",
       "Q 6.59375 17.96875 6.59375 36.375 \n",
       "Q 6.59375 54.828125 13.0625 64.515625 \n",
       "Q 19.53125 74.21875 31.78125 74.21875 \n",
       "z\n",
       "\" id=\"DejaVuSans-30\"/>\n",
       "       <path d=\"M 10.6875 12.40625 \n",
       "L 21 12.40625 \n",
       "L 21 0 \n",
       "L 10.6875 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-2e\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(44.70696 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"97.044886\" xlink:href=\"#md8a65da662\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- 0.5 -->\n",
       "      <defs>\n",
       "       <path d=\"M 10.796875 72.90625 \n",
       "L 49.515625 72.90625 \n",
       "L 49.515625 64.59375 \n",
       "L 19.828125 64.59375 \n",
       "L 19.828125 46.734375 \n",
       "Q 21.96875 47.46875 24.109375 47.828125 \n",
       "Q 26.265625 48.1875 28.421875 48.1875 \n",
       "Q 40.625 48.1875 47.75 41.5 \n",
       "Q 54.890625 34.8125 54.890625 23.390625 \n",
       "Q 54.890625 11.625 47.5625 5.09375 \n",
       "Q 40.234375 -1.421875 26.90625 -1.421875 \n",
       "Q 22.3125 -1.421875 17.546875 -0.640625 \n",
       "Q 12.796875 0.140625 7.71875 1.703125 \n",
       "L 7.71875 11.625 \n",
       "Q 12.109375 9.234375 16.796875 8.0625 \n",
       "Q 21.484375 6.890625 26.703125 6.890625 \n",
       "Q 35.15625 6.890625 40.078125 11.328125 \n",
       "Q 45.015625 15.765625 45.015625 23.390625 \n",
       "Q 45.015625 31 40.078125 35.4375 \n",
       "Q 35.15625 39.890625 26.703125 39.890625 \n",
       "Q 22.75 39.890625 18.8125 39.015625 \n",
       "Q 14.890625 38.140625 10.796875 36.28125 \n",
       "z\n",
       "\" id=\"DejaVuSans-35\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(89.093324 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"141.43125\" xlink:href=\"#md8a65da662\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 1.0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 12.40625 8.296875 \n",
       "L 28.515625 8.296875 \n",
       "L 28.515625 63.921875 \n",
       "L 10.984375 60.40625 \n",
       "L 10.984375 69.390625 \n",
       "L 28.421875 72.90625 \n",
       "L 38.28125 72.90625 \n",
       "L 38.28125 8.296875 \n",
       "L 54.390625 8.296875 \n",
       "L 54.390625 0 \n",
       "L 12.40625 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-31\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(133.479688 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"185.817614\" xlink:href=\"#md8a65da662\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 1.5 -->\n",
       "      <g transform=\"translate(177.866051 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"230.203977\" xlink:href=\"#md8a65da662\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- 2.0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 19.1875 8.296875 \n",
       "L 53.609375 8.296875 \n",
       "L 53.609375 0 \n",
       "L 7.328125 0 \n",
       "L 7.328125 8.296875 \n",
       "Q 12.9375 14.109375 22.625 23.890625 \n",
       "Q 32.328125 33.6875 34.8125 36.53125 \n",
       "Q 39.546875 41.84375 41.421875 45.53125 \n",
       "Q 43.3125 49.21875 43.3125 52.78125 \n",
       "Q 43.3125 58.59375 39.234375 62.25 \n",
       "Q 35.15625 65.921875 28.609375 65.921875 \n",
       "Q 23.96875 65.921875 18.8125 64.3125 \n",
       "Q 13.671875 62.703125 7.8125 59.421875 \n",
       "L 7.8125 69.390625 \n",
       "Q 13.765625 71.78125 18.9375 73 \n",
       "Q 24.125 74.21875 28.421875 74.21875 \n",
       "Q 39.75 74.21875 46.484375 68.546875 \n",
       "Q 53.21875 62.890625 53.21875 53.421875 \n",
       "Q 53.21875 48.921875 51.53125 44.890625 \n",
       "Q 49.859375 40.875 45.40625 35.40625 \n",
       "Q 44.1875 33.984375 37.640625 27.21875 \n",
       "Q 31.109375 20.453125 19.1875 8.296875 \n",
       "z\n",
       "\" id=\"DejaVuSans-32\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(222.252415 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_6\">\n",
       "     <!-- epoch -->\n",
       "     <defs>\n",
       "      <path d=\"M 56.203125 29.59375 \n",
       "L 56.203125 25.203125 \n",
       "L 14.890625 25.203125 \n",
       "Q 15.484375 15.921875 20.484375 11.0625 \n",
       "Q 25.484375 6.203125 34.421875 6.203125 \n",
       "Q 39.59375 6.203125 44.453125 7.46875 \n",
       "Q 49.3125 8.734375 54.109375 11.28125 \n",
       "L 54.109375 2.78125 \n",
       "Q 49.265625 0.734375 44.1875 -0.34375 \n",
       "Q 39.109375 -1.421875 33.890625 -1.421875 \n",
       "Q 20.796875 -1.421875 13.15625 6.1875 \n",
       "Q 5.515625 13.8125 5.515625 26.8125 \n",
       "Q 5.515625 40.234375 12.765625 48.109375 \n",
       "Q 20.015625 56 32.328125 56 \n",
       "Q 43.359375 56 49.78125 48.890625 \n",
       "Q 56.203125 41.796875 56.203125 29.59375 \n",
       "z\n",
       "M 47.21875 32.234375 \n",
       "Q 47.125 39.59375 43.09375 43.984375 \n",
       "Q 39.0625 48.390625 32.421875 48.390625 \n",
       "Q 24.90625 48.390625 20.390625 44.140625 \n",
       "Q 15.875 39.890625 15.1875 32.171875 \n",
       "z\n",
       "\" id=\"DejaVuSans-65\"/>\n",
       "      <path d=\"M 18.109375 8.203125 \n",
       "L 18.109375 -20.796875 \n",
       "L 9.078125 -20.796875 \n",
       "L 9.078125 54.6875 \n",
       "L 18.109375 54.6875 \n",
       "L 18.109375 46.390625 \n",
       "Q 20.953125 51.265625 25.265625 53.625 \n",
       "Q 29.59375 56 35.59375 56 \n",
       "Q 45.5625 56 51.78125 48.09375 \n",
       "Q 58.015625 40.1875 58.015625 27.296875 \n",
       "Q 58.015625 14.40625 51.78125 6.484375 \n",
       "Q 45.5625 -1.421875 35.59375 -1.421875 \n",
       "Q 29.59375 -1.421875 25.265625 0.953125 \n",
       "Q 20.953125 3.328125 18.109375 8.203125 \n",
       "z\n",
       "M 48.6875 27.296875 \n",
       "Q 48.6875 37.203125 44.609375 42.84375 \n",
       "Q 40.53125 48.484375 33.40625 48.484375 \n",
       "Q 26.265625 48.484375 22.1875 42.84375 \n",
       "Q 18.109375 37.203125 18.109375 27.296875 \n",
       "Q 18.109375 17.390625 22.1875 11.75 \n",
       "Q 26.265625 6.109375 33.40625 6.109375 \n",
       "Q 40.53125 6.109375 44.609375 11.75 \n",
       "Q 48.6875 17.390625 48.6875 27.296875 \n",
       "z\n",
       "\" id=\"DejaVuSans-70\"/>\n",
       "      <path d=\"M 30.609375 48.390625 \n",
       "Q 23.390625 48.390625 19.1875 42.75 \n",
       "Q 14.984375 37.109375 14.984375 27.296875 \n",
       "Q 14.984375 17.484375 19.15625 11.84375 \n",
       "Q 23.34375 6.203125 30.609375 6.203125 \n",
       "Q 37.796875 6.203125 41.984375 11.859375 \n",
       "Q 46.1875 17.53125 46.1875 27.296875 \n",
       "Q 46.1875 37.015625 41.984375 42.703125 \n",
       "Q 37.796875 48.390625 30.609375 48.390625 \n",
       "z\n",
       "M 30.609375 56 \n",
       "Q 42.328125 56 49.015625 48.375 \n",
       "Q 55.71875 40.765625 55.71875 27.296875 \n",
       "Q 55.71875 13.875 49.015625 6.21875 \n",
       "Q 42.328125 -1.421875 30.609375 -1.421875 \n",
       "Q 18.84375 -1.421875 12.171875 6.21875 \n",
       "Q 5.515625 13.875 5.515625 27.296875 \n",
       "Q 5.515625 40.765625 12.171875 48.375 \n",
       "Q 18.84375 56 30.609375 56 \n",
       "z\n",
       "\" id=\"DejaVuSans-6f\"/>\n",
       "      <path d=\"M 48.78125 52.59375 \n",
       "L 48.78125 44.1875 \n",
       "Q 44.96875 46.296875 41.140625 47.34375 \n",
       "Q 37.3125 48.390625 33.40625 48.390625 \n",
       "Q 24.65625 48.390625 19.8125 42.84375 \n",
       "Q 14.984375 37.3125 14.984375 27.296875 \n",
       "Q 14.984375 17.28125 19.8125 11.734375 \n",
       "Q 24.65625 6.203125 33.40625 6.203125 \n",
       "Q 37.3125 6.203125 41.140625 7.25 \n",
       "Q 44.96875 8.296875 48.78125 10.40625 \n",
       "L 48.78125 2.09375 \n",
       "Q 45.015625 0.34375 40.984375 -0.53125 \n",
       "Q 36.96875 -1.421875 32.421875 -1.421875 \n",
       "Q 20.0625 -1.421875 12.78125 6.34375 \n",
       "Q 5.515625 14.109375 5.515625 27.296875 \n",
       "Q 5.515625 40.671875 12.859375 48.328125 \n",
       "Q 20.21875 56 33.015625 56 \n",
       "Q 37.15625 56 41.109375 55.140625 \n",
       "Q 45.0625 54.296875 48.78125 52.59375 \n",
       "z\n",
       "\" id=\"DejaVuSans-63\"/>\n",
       "      <path d=\"M 54.890625 33.015625 \n",
       "L 54.890625 0 \n",
       "L 45.90625 0 \n",
       "L 45.90625 32.71875 \n",
       "Q 45.90625 40.484375 42.875 44.328125 \n",
       "Q 39.84375 48.1875 33.796875 48.1875 \n",
       "Q 26.515625 48.1875 22.3125 43.546875 \n",
       "Q 18.109375 38.921875 18.109375 30.90625 \n",
       "L 18.109375 0 \n",
       "L 9.078125 0 \n",
       "L 9.078125 75.984375 \n",
       "L 18.109375 75.984375 \n",
       "L 18.109375 46.1875 \n",
       "Q 21.34375 51.125 25.703125 53.5625 \n",
       "Q 30.078125 56 35.796875 56 \n",
       "Q 45.21875 56 50.046875 50.171875 \n",
       "Q 54.890625 44.34375 54.890625 33.015625 \n",
       "z\n",
       "\" id=\"DejaVuSans-68\"/>\n",
       "     </defs>\n",
       "     <g transform=\"translate(126.203125 174.876562)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use x=\"61.523438\" xlink:href=\"#DejaVuSans-70\"/>\n",
       "      <use x=\"125\" xlink:href=\"#DejaVuSans-6f\"/>\n",
       "      <use x=\"186.181641\" xlink:href=\"#DejaVuSans-63\"/>\n",
       "      <use x=\"241.162109\" xlink:href=\"#DejaVuSans-68\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m93fe4a6289\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m93fe4a6289\" y=\"114.591783\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- 0.3 -->\n",
       "      <defs>\n",
       "       <path d=\"M 40.578125 39.3125 \n",
       "Q 47.65625 37.796875 51.625 33 \n",
       "Q 55.609375 28.21875 55.609375 21.1875 \n",
       "Q 55.609375 10.40625 48.1875 4.484375 \n",
       "Q 40.765625 -1.421875 27.09375 -1.421875 \n",
       "Q 22.515625 -1.421875 17.65625 -0.515625 \n",
       "Q 12.796875 0.390625 7.625 2.203125 \n",
       "L 7.625 11.71875 \n",
       "Q 11.71875 9.328125 16.59375 8.109375 \n",
       "Q 21.484375 6.890625 26.8125 6.890625 \n",
       "Q 36.078125 6.890625 40.9375 10.546875 \n",
       "Q 45.796875 14.203125 45.796875 21.1875 \n",
       "Q 45.796875 27.640625 41.28125 31.265625 \n",
       "Q 36.765625 34.90625 28.71875 34.90625 \n",
       "L 20.21875 34.90625 \n",
       "L 20.21875 43.015625 \n",
       "L 29.109375 43.015625 \n",
       "Q 36.375 43.015625 40.234375 45.921875 \n",
       "Q 44.09375 48.828125 44.09375 54.296875 \n",
       "Q 44.09375 59.90625 40.109375 62.90625 \n",
       "Q 36.140625 65.921875 28.71875 65.921875 \n",
       "Q 24.65625 65.921875 20.015625 65.03125 \n",
       "Q 15.375 64.15625 9.8125 62.3125 \n",
       "L 9.8125 71.09375 \n",
       "Q 15.4375 72.65625 20.34375 73.4375 \n",
       "Q 25.25 74.21875 29.59375 74.21875 \n",
       "Q 40.828125 74.21875 47.359375 69.109375 \n",
       "Q 53.90625 64.015625 53.90625 55.328125 \n",
       "Q 53.90625 49.265625 50.4375 45.09375 \n",
       "Q 46.96875 40.921875 40.578125 39.3125 \n",
       "z\n",
       "\" id=\"DejaVuSans-33\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(20.878125 118.391002)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-33\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m93fe4a6289\" y=\"69.613811\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 0.4 -->\n",
       "      <defs>\n",
       "       <path d=\"M 37.796875 64.3125 \n",
       "L 12.890625 25.390625 \n",
       "L 37.796875 25.390625 \n",
       "z\n",
       "M 35.203125 72.90625 \n",
       "L 47.609375 72.90625 \n",
       "L 47.609375 25.390625 \n",
       "L 58.015625 25.390625 \n",
       "L 58.015625 17.1875 \n",
       "L 47.609375 17.1875 \n",
       "L 47.609375 0 \n",
       "L 37.796875 0 \n",
       "L 37.796875 17.1875 \n",
       "L 4.890625 17.1875 \n",
       "L 4.890625 26.703125 \n",
       "z\n",
       "\" id=\"DejaVuSans-34\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(20.878125 73.41303)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-34\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"43.78125\" xlink:href=\"#m93fe4a6289\" y=\"24.635839\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <!-- 0.5 -->\n",
       "      <g transform=\"translate(20.878125 28.435058)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_10\">\n",
       "     <!-- loss -->\n",
       "     <defs>\n",
       "      <path d=\"M 9.421875 75.984375 \n",
       "L 18.40625 75.984375 \n",
       "L 18.40625 0 \n",
       "L 9.421875 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-6c\"/>\n",
       "      <path d=\"M 44.28125 53.078125 \n",
       "L 44.28125 44.578125 \n",
       "Q 40.484375 46.53125 36.375 47.5 \n",
       "Q 32.28125 48.484375 27.875 48.484375 \n",
       "Q 21.1875 48.484375 17.84375 46.4375 \n",
       "Q 14.5 44.390625 14.5 40.28125 \n",
       "Q 14.5 37.15625 16.890625 35.375 \n",
       "Q 19.28125 33.59375 26.515625 31.984375 \n",
       "L 29.59375 31.296875 \n",
       "Q 39.15625 29.25 43.1875 25.515625 \n",
       "Q 47.21875 21.78125 47.21875 15.09375 \n",
       "Q 47.21875 7.46875 41.1875 3.015625 \n",
       "Q 35.15625 -1.421875 24.609375 -1.421875 \n",
       "Q 20.21875 -1.421875 15.453125 -0.5625 \n",
       "Q 10.6875 0.296875 5.421875 2 \n",
       "L 5.421875 11.28125 \n",
       "Q 10.40625 8.6875 15.234375 7.390625 \n",
       "Q 20.0625 6.109375 24.8125 6.109375 \n",
       "Q 31.15625 6.109375 34.5625 8.28125 \n",
       "Q 37.984375 10.453125 37.984375 14.40625 \n",
       "Q 37.984375 18.0625 35.515625 20.015625 \n",
       "Q 33.0625 21.96875 24.703125 23.78125 \n",
       "L 21.578125 24.515625 \n",
       "Q 13.234375 26.265625 9.515625 29.90625 \n",
       "Q 5.8125 33.546875 5.8125 39.890625 \n",
       "Q 5.8125 47.609375 11.28125 51.796875 \n",
       "Q 16.75 56 26.8125 56 \n",
       "Q 31.78125 56 36.171875 55.265625 \n",
       "Q 40.578125 54.546875 44.28125 53.078125 \n",
       "z\n",
       "\" id=\"DejaVuSans-73\"/>\n",
       "     </defs>\n",
       "     <g transform=\"translate(14.798437 88.307812)rotate(-90)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-6c\"/>\n",
       "      <use x=\"27.783203\" xlink:href=\"#DejaVuSans-6f\"/>\n",
       "      <use x=\"88.964844\" xlink:href=\"#DejaVuSans-73\"/>\n",
       "      <use x=\"141.064453\" xlink:href=\"#DejaVuSans-73\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_9\">\n",
       "    <path clip-path=\"url(#pf6779682bb)\" d=\"M 52.658523 16.877273 \n",
       "L 58.576705 106.644664 \n",
       "L 64.494886 132.718295 \n",
       "L 70.413068 134.348132 \n",
       "L 76.33125 138.786563 \n",
       "L 82.249432 137.706524 \n",
       "L 88.167614 139.268957 \n",
       "L 94.085795 139.871897 \n",
       "L 100.003977 138.433817 \n",
       "L 105.922159 139.203918 \n",
       "L 111.840341 140.031162 \n",
       "L 117.758523 139.256759 \n",
       "L 123.676705 139.395743 \n",
       "L 129.594886 139.700836 \n",
       "L 135.513068 139.896447 \n",
       "L 141.43125 140.137299 \n",
       "L 147.349432 140.068648 \n",
       "L 153.267614 139.722893 \n",
       "L 159.185795 139.766484 \n",
       "L 165.103977 140.189302 \n",
       "L 171.022159 140.097716 \n",
       "L 176.940341 139.447096 \n",
       "L 182.858523 139.819733 \n",
       "L 188.776705 140.281974 \n",
       "L 194.694886 140.141903 \n",
       "L 200.613068 140.080337 \n",
       "L 206.53125 140.422727 \n",
       "L 212.449432 140.182552 \n",
       "L 218.367614 140.304627 \n",
       "L 224.285795 139.94347 \n",
       "L 230.203977 139.961942 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 43.78125 146.6 \n",
       "L 43.78125 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 239.08125 146.6 \n",
       "L 239.08125 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 43.78125 146.6 \n",
       "L 239.08125 146.6 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 43.78125 10.7 \n",
       "L 239.08125 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"pf6779682bb\">\n",
       "   <rect height=\"135.9\" width=\"195.3\" x=\"43.78125\" y=\"10.7\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 252x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "d2l.train_ch7(adagrad, init_adagrad_states(), {'lr': 0.1}, features, labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简洁实现\n",
    "\n",
    "通过名称为“adagrad”的`Trainer`实例，我们便可使用Gluon提供的AdaGrad算法来训练模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "attributes": {
     "classes": [],
     "id": "",
     "n": "6"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loss: 0.243284, 0.413352 sec per epoch\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Created with matplotlib (http://matplotlib.org/) -->\n",
       "<svg height=\"184.15625pt\" version=\"1.1\" viewBox=\"0 0 256.14375 184.15625\" width=\"256.14375pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <defs>\n",
       "  <style type=\"text/css\">\n",
       "*{stroke-linecap:butt;stroke-linejoin:round;}\n",
       "  </style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 184.15625 \n",
       "L 256.14375 184.15625 \n",
       "L 256.14375 -0 \n",
       "L 0 -0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 50.14375 146.6 \n",
       "L 245.44375 146.6 \n",
       "L 245.44375 10.7 \n",
       "L 50.14375 10.7 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"m5226f22b13\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"59.021023\" xlink:href=\"#m5226f22b13\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- 0.0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 31.78125 66.40625 \n",
       "Q 24.171875 66.40625 20.328125 58.90625 \n",
       "Q 16.5 51.421875 16.5 36.375 \n",
       "Q 16.5 21.390625 20.328125 13.890625 \n",
       "Q 24.171875 6.390625 31.78125 6.390625 \n",
       "Q 39.453125 6.390625 43.28125 13.890625 \n",
       "Q 47.125 21.390625 47.125 36.375 \n",
       "Q 47.125 51.421875 43.28125 58.90625 \n",
       "Q 39.453125 66.40625 31.78125 66.40625 \n",
       "z\n",
       "M 31.78125 74.21875 \n",
       "Q 44.046875 74.21875 50.515625 64.515625 \n",
       "Q 56.984375 54.828125 56.984375 36.375 \n",
       "Q 56.984375 17.96875 50.515625 8.265625 \n",
       "Q 44.046875 -1.421875 31.78125 -1.421875 \n",
       "Q 19.53125 -1.421875 13.0625 8.265625 \n",
       "Q 6.59375 17.96875 6.59375 36.375 \n",
       "Q 6.59375 54.828125 13.0625 64.515625 \n",
       "Q 19.53125 74.21875 31.78125 74.21875 \n",
       "z\n",
       "\" id=\"DejaVuSans-30\"/>\n",
       "       <path d=\"M 10.6875 12.40625 \n",
       "L 21 12.40625 \n",
       "L 21 0 \n",
       "L 10.6875 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-2e\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(51.06946 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"103.407386\" xlink:href=\"#m5226f22b13\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- 0.5 -->\n",
       "      <defs>\n",
       "       <path d=\"M 10.796875 72.90625 \n",
       "L 49.515625 72.90625 \n",
       "L 49.515625 64.59375 \n",
       "L 19.828125 64.59375 \n",
       "L 19.828125 46.734375 \n",
       "Q 21.96875 47.46875 24.109375 47.828125 \n",
       "Q 26.265625 48.1875 28.421875 48.1875 \n",
       "Q 40.625 48.1875 47.75 41.5 \n",
       "Q 54.890625 34.8125 54.890625 23.390625 \n",
       "Q 54.890625 11.625 47.5625 5.09375 \n",
       "Q 40.234375 -1.421875 26.90625 -1.421875 \n",
       "Q 22.3125 -1.421875 17.546875 -0.640625 \n",
       "Q 12.796875 0.140625 7.71875 1.703125 \n",
       "L 7.71875 11.625 \n",
       "Q 12.109375 9.234375 16.796875 8.0625 \n",
       "Q 21.484375 6.890625 26.703125 6.890625 \n",
       "Q 35.15625 6.890625 40.078125 11.328125 \n",
       "Q 45.015625 15.765625 45.015625 23.390625 \n",
       "Q 45.015625 31 40.078125 35.4375 \n",
       "Q 35.15625 39.890625 26.703125 39.890625 \n",
       "Q 22.75 39.890625 18.8125 39.015625 \n",
       "Q 14.890625 38.140625 10.796875 36.28125 \n",
       "z\n",
       "\" id=\"DejaVuSans-35\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(95.455824 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"147.79375\" xlink:href=\"#m5226f22b13\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 1.0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 12.40625 8.296875 \n",
       "L 28.515625 8.296875 \n",
       "L 28.515625 63.921875 \n",
       "L 10.984375 60.40625 \n",
       "L 10.984375 69.390625 \n",
       "L 28.421875 72.90625 \n",
       "L 38.28125 72.90625 \n",
       "L 38.28125 8.296875 \n",
       "L 54.390625 8.296875 \n",
       "L 54.390625 0 \n",
       "L 12.40625 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-31\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(139.842187 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"192.180114\" xlink:href=\"#m5226f22b13\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 1.5 -->\n",
       "      <g transform=\"translate(184.228551 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"236.566477\" xlink:href=\"#m5226f22b13\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <!-- 2.0 -->\n",
       "      <defs>\n",
       "       <path d=\"M 19.1875 8.296875 \n",
       "L 53.609375 8.296875 \n",
       "L 53.609375 0 \n",
       "L 7.328125 0 \n",
       "L 7.328125 8.296875 \n",
       "Q 12.9375 14.109375 22.625 23.890625 \n",
       "Q 32.328125 33.6875 34.8125 36.53125 \n",
       "Q 39.546875 41.84375 41.421875 45.53125 \n",
       "Q 43.3125 49.21875 43.3125 52.78125 \n",
       "Q 43.3125 58.59375 39.234375 62.25 \n",
       "Q 35.15625 65.921875 28.609375 65.921875 \n",
       "Q 23.96875 65.921875 18.8125 64.3125 \n",
       "Q 13.671875 62.703125 7.8125 59.421875 \n",
       "L 7.8125 69.390625 \n",
       "Q 13.765625 71.78125 18.9375 73 \n",
       "Q 24.125 74.21875 28.421875 74.21875 \n",
       "Q 39.75 74.21875 46.484375 68.546875 \n",
       "Q 53.21875 62.890625 53.21875 53.421875 \n",
       "Q 53.21875 48.921875 51.53125 44.890625 \n",
       "Q 49.859375 40.875 45.40625 35.40625 \n",
       "Q 44.1875 33.984375 37.640625 27.21875 \n",
       "Q 31.109375 20.453125 19.1875 8.296875 \n",
       "z\n",
       "\" id=\"DejaVuSans-32\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(228.614915 161.198437)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_6\">\n",
       "     <!-- epoch -->\n",
       "     <defs>\n",
       "      <path d=\"M 56.203125 29.59375 \n",
       "L 56.203125 25.203125 \n",
       "L 14.890625 25.203125 \n",
       "Q 15.484375 15.921875 20.484375 11.0625 \n",
       "Q 25.484375 6.203125 34.421875 6.203125 \n",
       "Q 39.59375 6.203125 44.453125 7.46875 \n",
       "Q 49.3125 8.734375 54.109375 11.28125 \n",
       "L 54.109375 2.78125 \n",
       "Q 49.265625 0.734375 44.1875 -0.34375 \n",
       "Q 39.109375 -1.421875 33.890625 -1.421875 \n",
       "Q 20.796875 -1.421875 13.15625 6.1875 \n",
       "Q 5.515625 13.8125 5.515625 26.8125 \n",
       "Q 5.515625 40.234375 12.765625 48.109375 \n",
       "Q 20.015625 56 32.328125 56 \n",
       "Q 43.359375 56 49.78125 48.890625 \n",
       "Q 56.203125 41.796875 56.203125 29.59375 \n",
       "z\n",
       "M 47.21875 32.234375 \n",
       "Q 47.125 39.59375 43.09375 43.984375 \n",
       "Q 39.0625 48.390625 32.421875 48.390625 \n",
       "Q 24.90625 48.390625 20.390625 44.140625 \n",
       "Q 15.875 39.890625 15.1875 32.171875 \n",
       "z\n",
       "\" id=\"DejaVuSans-65\"/>\n",
       "      <path d=\"M 18.109375 8.203125 \n",
       "L 18.109375 -20.796875 \n",
       "L 9.078125 -20.796875 \n",
       "L 9.078125 54.6875 \n",
       "L 18.109375 54.6875 \n",
       "L 18.109375 46.390625 \n",
       "Q 20.953125 51.265625 25.265625 53.625 \n",
       "Q 29.59375 56 35.59375 56 \n",
       "Q 45.5625 56 51.78125 48.09375 \n",
       "Q 58.015625 40.1875 58.015625 27.296875 \n",
       "Q 58.015625 14.40625 51.78125 6.484375 \n",
       "Q 45.5625 -1.421875 35.59375 -1.421875 \n",
       "Q 29.59375 -1.421875 25.265625 0.953125 \n",
       "Q 20.953125 3.328125 18.109375 8.203125 \n",
       "z\n",
       "M 48.6875 27.296875 \n",
       "Q 48.6875 37.203125 44.609375 42.84375 \n",
       "Q 40.53125 48.484375 33.40625 48.484375 \n",
       "Q 26.265625 48.484375 22.1875 42.84375 \n",
       "Q 18.109375 37.203125 18.109375 27.296875 \n",
       "Q 18.109375 17.390625 22.1875 11.75 \n",
       "Q 26.265625 6.109375 33.40625 6.109375 \n",
       "Q 40.53125 6.109375 44.609375 11.75 \n",
       "Q 48.6875 17.390625 48.6875 27.296875 \n",
       "z\n",
       "\" id=\"DejaVuSans-70\"/>\n",
       "      <path d=\"M 30.609375 48.390625 \n",
       "Q 23.390625 48.390625 19.1875 42.75 \n",
       "Q 14.984375 37.109375 14.984375 27.296875 \n",
       "Q 14.984375 17.484375 19.15625 11.84375 \n",
       "Q 23.34375 6.203125 30.609375 6.203125 \n",
       "Q 37.796875 6.203125 41.984375 11.859375 \n",
       "Q 46.1875 17.53125 46.1875 27.296875 \n",
       "Q 46.1875 37.015625 41.984375 42.703125 \n",
       "Q 37.796875 48.390625 30.609375 48.390625 \n",
       "z\n",
       "M 30.609375 56 \n",
       "Q 42.328125 56 49.015625 48.375 \n",
       "Q 55.71875 40.765625 55.71875 27.296875 \n",
       "Q 55.71875 13.875 49.015625 6.21875 \n",
       "Q 42.328125 -1.421875 30.609375 -1.421875 \n",
       "Q 18.84375 -1.421875 12.171875 6.21875 \n",
       "Q 5.515625 13.875 5.515625 27.296875 \n",
       "Q 5.515625 40.765625 12.171875 48.375 \n",
       "Q 18.84375 56 30.609375 56 \n",
       "z\n",
       "\" id=\"DejaVuSans-6f\"/>\n",
       "      <path d=\"M 48.78125 52.59375 \n",
       "L 48.78125 44.1875 \n",
       "Q 44.96875 46.296875 41.140625 47.34375 \n",
       "Q 37.3125 48.390625 33.40625 48.390625 \n",
       "Q 24.65625 48.390625 19.8125 42.84375 \n",
       "Q 14.984375 37.3125 14.984375 27.296875 \n",
       "Q 14.984375 17.28125 19.8125 11.734375 \n",
       "Q 24.65625 6.203125 33.40625 6.203125 \n",
       "Q 37.3125 6.203125 41.140625 7.25 \n",
       "Q 44.96875 8.296875 48.78125 10.40625 \n",
       "L 48.78125 2.09375 \n",
       "Q 45.015625 0.34375 40.984375 -0.53125 \n",
       "Q 36.96875 -1.421875 32.421875 -1.421875 \n",
       "Q 20.0625 -1.421875 12.78125 6.34375 \n",
       "Q 5.515625 14.109375 5.515625 27.296875 \n",
       "Q 5.515625 40.671875 12.859375 48.328125 \n",
       "Q 20.21875 56 33.015625 56 \n",
       "Q 37.15625 56 41.109375 55.140625 \n",
       "Q 45.0625 54.296875 48.78125 52.59375 \n",
       "z\n",
       "\" id=\"DejaVuSans-63\"/>\n",
       "      <path d=\"M 54.890625 33.015625 \n",
       "L 54.890625 0 \n",
       "L 45.90625 0 \n",
       "L 45.90625 32.71875 \n",
       "Q 45.90625 40.484375 42.875 44.328125 \n",
       "Q 39.84375 48.1875 33.796875 48.1875 \n",
       "Q 26.515625 48.1875 22.3125 43.546875 \n",
       "Q 18.109375 38.921875 18.109375 30.90625 \n",
       "L 18.109375 0 \n",
       "L 9.078125 0 \n",
       "L 9.078125 75.984375 \n",
       "L 18.109375 75.984375 \n",
       "L 18.109375 46.1875 \n",
       "Q 21.34375 51.125 25.703125 53.5625 \n",
       "Q 30.078125 56 35.796875 56 \n",
       "Q 45.21875 56 50.046875 50.171875 \n",
       "Q 54.890625 44.34375 54.890625 33.015625 \n",
       "z\n",
       "\" id=\"DejaVuSans-68\"/>\n",
       "     </defs>\n",
       "     <g transform=\"translate(132.565625 174.876562)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use x=\"61.523438\" xlink:href=\"#DejaVuSans-70\"/>\n",
       "      <use x=\"125\" xlink:href=\"#DejaVuSans-6f\"/>\n",
       "      <use x=\"186.181641\" xlink:href=\"#DejaVuSans-63\"/>\n",
       "      <use x=\"241.162109\" xlink:href=\"#DejaVuSans-68\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m4b3d528463\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"50.14375\" xlink:href=\"#m4b3d528463\" y=\"136.841668\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- 0.25 -->\n",
       "      <g transform=\"translate(20.878125 140.640887)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-32\"/>\n",
       "       <use x=\"159.033203\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"50.14375\" xlink:href=\"#m4b3d528463\" y=\"111.350957\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 0.30 -->\n",
       "      <defs>\n",
       "       <path d=\"M 40.578125 39.3125 \n",
       "Q 47.65625 37.796875 51.625 33 \n",
       "Q 55.609375 28.21875 55.609375 21.1875 \n",
       "Q 55.609375 10.40625 48.1875 4.484375 \n",
       "Q 40.765625 -1.421875 27.09375 -1.421875 \n",
       "Q 22.515625 -1.421875 17.65625 -0.515625 \n",
       "Q 12.796875 0.390625 7.625 2.203125 \n",
       "L 7.625 11.71875 \n",
       "Q 11.71875 9.328125 16.59375 8.109375 \n",
       "Q 21.484375 6.890625 26.8125 6.890625 \n",
       "Q 36.078125 6.890625 40.9375 10.546875 \n",
       "Q 45.796875 14.203125 45.796875 21.1875 \n",
       "Q 45.796875 27.640625 41.28125 31.265625 \n",
       "Q 36.765625 34.90625 28.71875 34.90625 \n",
       "L 20.21875 34.90625 \n",
       "L 20.21875 43.015625 \n",
       "L 29.109375 43.015625 \n",
       "Q 36.375 43.015625 40.234375 45.921875 \n",
       "Q 44.09375 48.828125 44.09375 54.296875 \n",
       "Q 44.09375 59.90625 40.109375 62.90625 \n",
       "Q 36.140625 65.921875 28.71875 65.921875 \n",
       "Q 24.65625 65.921875 20.015625 65.03125 \n",
       "Q 15.375 64.15625 9.8125 62.3125 \n",
       "L 9.8125 71.09375 \n",
       "Q 15.4375 72.65625 20.34375 73.4375 \n",
       "Q 25.25 74.21875 29.59375 74.21875 \n",
       "Q 40.828125 74.21875 47.359375 69.109375 \n",
       "Q 53.90625 64.015625 53.90625 55.328125 \n",
       "Q 53.90625 49.265625 50.4375 45.09375 \n",
       "Q 46.96875 40.921875 40.578125 39.3125 \n",
       "z\n",
       "\" id=\"DejaVuSans-33\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(20.878125 115.150175)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-33\"/>\n",
       "       <use x=\"159.033203\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"50.14375\" xlink:href=\"#m4b3d528463\" y=\"85.860245\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <!-- 0.35 -->\n",
       "      <g transform=\"translate(20.878125 89.659464)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-33\"/>\n",
       "       <use x=\"159.033203\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_9\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"50.14375\" xlink:href=\"#m4b3d528463\" y=\"60.369534\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <!-- 0.40 -->\n",
       "      <defs>\n",
       "       <path d=\"M 37.796875 64.3125 \n",
       "L 12.890625 25.390625 \n",
       "L 37.796875 25.390625 \n",
       "z\n",
       "M 35.203125 72.90625 \n",
       "L 47.609375 72.90625 \n",
       "L 47.609375 25.390625 \n",
       "L 58.015625 25.390625 \n",
       "L 58.015625 17.1875 \n",
       "L 47.609375 17.1875 \n",
       "L 47.609375 0 \n",
       "L 37.796875 0 \n",
       "L 37.796875 17.1875 \n",
       "L 4.890625 17.1875 \n",
       "L 4.890625 26.703125 \n",
       "z\n",
       "\" id=\"DejaVuSans-34\"/>\n",
       "      </defs>\n",
       "      <g transform=\"translate(20.878125 64.168753)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-34\"/>\n",
       "       <use x=\"159.033203\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"50.14375\" xlink:href=\"#m4b3d528463\" y=\"34.878822\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <!-- 0.45 -->\n",
       "      <g transform=\"translate(20.878125 38.678041)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/>\n",
       "       <use x=\"95.410156\" xlink:href=\"#DejaVuSans-34\"/>\n",
       "       <use x=\"159.033203\" xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_12\">\n",
       "     <!-- loss -->\n",
       "     <defs>\n",
       "      <path d=\"M 9.421875 75.984375 \n",
       "L 18.40625 75.984375 \n",
       "L 18.40625 0 \n",
       "L 9.421875 0 \n",
       "z\n",
       "\" id=\"DejaVuSans-6c\"/>\n",
       "      <path d=\"M 44.28125 53.078125 \n",
       "L 44.28125 44.578125 \n",
       "Q 40.484375 46.53125 36.375 47.5 \n",
       "Q 32.28125 48.484375 27.875 48.484375 \n",
       "Q 21.1875 48.484375 17.84375 46.4375 \n",
       "Q 14.5 44.390625 14.5 40.28125 \n",
       "Q 14.5 37.15625 16.890625 35.375 \n",
       "Q 19.28125 33.59375 26.515625 31.984375 \n",
       "L 29.59375 31.296875 \n",
       "Q 39.15625 29.25 43.1875 25.515625 \n",
       "Q 47.21875 21.78125 47.21875 15.09375 \n",
       "Q 47.21875 7.46875 41.1875 3.015625 \n",
       "Q 35.15625 -1.421875 24.609375 -1.421875 \n",
       "Q 20.21875 -1.421875 15.453125 -0.5625 \n",
       "Q 10.6875 0.296875 5.421875 2 \n",
       "L 5.421875 11.28125 \n",
       "Q 10.40625 8.6875 15.234375 7.390625 \n",
       "Q 20.0625 6.109375 24.8125 6.109375 \n",
       "Q 31.15625 6.109375 34.5625 8.28125 \n",
       "Q 37.984375 10.453125 37.984375 14.40625 \n",
       "Q 37.984375 18.0625 35.515625 20.015625 \n",
       "Q 33.0625 21.96875 24.703125 23.78125 \n",
       "L 21.578125 24.515625 \n",
       "Q 13.234375 26.265625 9.515625 29.90625 \n",
       "Q 5.8125 33.546875 5.8125 39.890625 \n",
       "Q 5.8125 47.609375 11.28125 51.796875 \n",
       "Q 16.75 56 26.8125 56 \n",
       "Q 31.78125 56 36.171875 55.265625 \n",
       "Q 40.578125 54.546875 44.28125 53.078125 \n",
       "z\n",
       "\" id=\"DejaVuSans-73\"/>\n",
       "     </defs>\n",
       "     <g transform=\"translate(14.798437 88.307812)rotate(-90)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-6c\"/>\n",
       "      <use x=\"27.783203\" xlink:href=\"#DejaVuSans-6f\"/>\n",
       "      <use x=\"88.964844\" xlink:href=\"#DejaVuSans-73\"/>\n",
       "      <use x=\"141.064453\" xlink:href=\"#DejaVuSans-73\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_11\">\n",
       "    <path clip-path=\"url(#p37b4127cb2)\" d=\"M 59.021023 16.877273 \n",
       "L 64.939205 107.37301 \n",
       "L 70.857386 123.998736 \n",
       "L 76.775568 131.441937 \n",
       "L 82.69375 133.78735 \n",
       "L 88.611932 136.501376 \n",
       "L 94.530114 138.863669 \n",
       "L 100.448295 135.523649 \n",
       "L 106.366477 137.058876 \n",
       "L 112.284659 136.167374 \n",
       "L 118.202841 138.303206 \n",
       "L 124.121023 139.696486 \n",
       "L 130.039205 139.583901 \n",
       "L 135.957386 138.440693 \n",
       "L 141.875568 139.388686 \n",
       "L 147.79375 139.996006 \n",
       "L 153.711932 139.706856 \n",
       "L 159.630114 138.779382 \n",
       "L 165.548295 139.888633 \n",
       "L 171.466477 140.001894 \n",
       "L 177.384659 137.915396 \n",
       "L 183.302841 140.269742 \n",
       "L 189.221023 139.899549 \n",
       "L 195.139205 139.485188 \n",
       "L 201.057386 140.054038 \n",
       "L 206.975568 140.422727 \n",
       "L 212.89375 140.313227 \n",
       "L 218.811932 140.063975 \n",
       "L 224.730114 139.826529 \n",
       "L 230.648295 139.808729 \n",
       "L 236.566477 140.265739 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 50.14375 146.6 \n",
       "L 50.14375 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 245.44375 146.6 \n",
       "L 245.44375 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 50.14375 146.6 \n",
       "L 245.44375 146.6 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 50.14375 10.7 \n",
       "L 245.44375 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p37b4127cb2\">\n",
       "   <rect height=\"135.9\" width=\"195.3\" x=\"50.14375\" y=\"10.7\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 252x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "d2l.train_gluon_ch7('adagrad', {'learning_rate': 0.1}, features, labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 小结\n",
    "\n",
    "* AdaGrad算法在迭代过程中不断调整学习率，并让目标函数自变量中每个元素都分别拥有自己的学习率。\n",
    "* 使用AdaGrad算法时，自变量中每个元素的学习率在迭代过程中一直在降低（或不变）。\n",
    "\n",
    "## 练习\n",
    "\n",
    "* 在介绍AdaGrad算法的特点时，我们提到了它可能存在的问题。你能想到什么办法来解决这个问题？\n",
    "* 在实验中尝试使用其他的初始学习率，结果有什么变化？\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## 参考文献\n",
    "\n",
    "[1] Duchi, J., Hazan, E., & Singer, Y. (2011). Adaptive subgradient methods for online learning and stochastic optimization. Journal of Machine Learning Research, 12(Jul), 2121-2159.\n",
    "\n",
    "## 扫码直达[讨论区](https://discuss.gluon.ai/t/topic/2273)\n",
    "\n",
    "![](../img/qr_adagrad.svg)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}